Java Core Mock Test-3

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Abiprakasam
A
Abiprakasam
Community Contributor
Quizzes Created: 1 | Total Attempts: 138
Questions: 35 | Attempts: 138

SettingsSettingsSettings
Java Core Mock Test-3 - Quiz


Access Specifers, Constructors and method


Questions and Answers
  • 1. 

    Which  access modifier have almost identical behaviour as that of default.?

    • A.

      Protected

    • B.

      Public

    • C.

      Private

    • D.

      Both public and protected

    Correct Answer
    A. Protected
    Explanation
    Protected access modifier has almost identical behavior as that of default access modifier. Both allow access to the members within the same package, but restrict access from outside the package. The main difference is that protected also allows access to subclasses, whereas default does not. Protected members can be accessed by subclasses and classes within the same package, just like default members, but they cannot be accessed by classes in different packages. Therefore, protected is the correct answer as it closely resembles the behavior of default access modifier.

    Rate this question:

  • 2. 

    Check the statements which are true.(Choose all that apply)

    • A.

      A member declared as ‘public’ CAN be accessed from ‘Other packages’.

    • B.

      A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.

    • C.

      A member declared with ‘protected’ access modifier CANNOT be accessed by the members of the ‘Other package’.

    • D.

      A member declared with ‘Default’ access modifier CANNOT be accessed by the members of the ‘subclass’.

    Correct Answer(s)
    A. A member declared as ‘public’ CAN be accessed from ‘Other packages’.
    B. A member declared with ‘protected’ access modifier CAN be accessed by the members of the ‘subclass’.
    D. A member declared with ‘Default’ access modifier CANNOT be accessed by the members of the ‘subclass’.
    Explanation
    The given answer is correct because it accurately describes the access modifiers in Java. A member declared as 'public' can be accessed from other packages, as it has the widest scope of accessibility. A member declared with 'protected' access modifier can be accessed by the members of the subclass, as well as by other classes in the same package. However, it cannot be accessed by the members of other packages. A member declared with 'default' access modifier can only be accessed by other classes in the same package, and not by the members of the subclass.

    Rate this question:

  • 3. 

    State True are False. Overriding does not depends on inheritance.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Overriding is a concept in object-oriented programming where a subclass provides a different implementation of a method that is already defined in its superclass. Inheritance is a fundamental principle in object-oriented programming, where a subclass inherits the properties and methods of its superclass. Overriding is only possible when there is a relationship of inheritance between classes, as the subclass needs to inherit the method from the superclass in order to override it. Therefore, overriding does depend on inheritance, making the statement false.

    Rate this question:

  • 4. 

    Consider the following code: class AT1 { public static void main (String[] args) { byte[] a = new byte[1]; long[] b = new long[1]; float[] c = new float[1]; Object[] d = new Object[1]; System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]); } } Which of the following will be the output of the above code?

    • A.

      Prints: 0,0,0,null

    • B.

      Run-time error

    • C.

      Prints: 0,0,0.0,null

    • D.

      Compile-time error 12 Consider the following code snippet: interface i1 { int i = 0; } interface i2 { int i = 0; } class inter implements i1, i2 { public static void main(String[] a) { System.out.println(i); } } Which of the following options will be the output of the above code snippet? Answer: a. Runtime

    Correct Answer
    C. Prints: 0,0,0.0,null
  • 5. 

    The following class definitions are in separate files. Note that the Widget and BigWidget classes are in different packages:  package conglomo;  public class Widget extends Object{   private int myWidth;  XXXXXX void setWidth( int n ) {   myWidth = n;   }   } // the following is in a separate file and in separate package  package conglomo.widgets;   import conglomo.Widget ;   public class BigWidget extends Widget {   BigWidget() {   setWidth( 204 );  }  } Which of the following modifiers, used in line 4 instead of XXXXXX, would allow the BigWidget class to access the setWidth method (as in line 12)? (Choose 2)

    • A.

      Final

    • B.

      Default (blank), that is, the method declaration would read void setWidth( int n )

    • C.

      Protected

    • D.

      Private

    • E.

      Public

    Correct Answer(s)
    C. Protected
    E. Public
    Explanation
    The "protected" and "public" modifiers, used in line 4 instead of XXXXXX, would allow the BigWidget class to access the setWidth method. The "protected" modifier allows access to the method within the same package and by subclasses, while the "public" modifier allows access from any class.

    Rate this question:

  • 6. 

    what do u mean by signatures in the function overloading?    

    • A.

      Return types

    • B.

      Parameters of functions

    • C.

      Datatypes of parameters

    • D.

      Properties

    Correct Answer
    C. Datatypes of parameters
    Explanation
    In function overloading, signatures refer to the combination of the function name, return type, and the number and types of parameters. It is used to differentiate between multiple functions with the same name but different parameters. By changing the data types of the parameters, we can create multiple functions with the same name but different behaviors. This allows for more flexibility and versatility in programming, as different versions of the same function can be used depending on the specific data types of the arguments passed.

    Rate this question:

  • 7. 

    interface Base { boolean m1 (); byte m2(short s); } which two code fragments will compile?

    • A.

      Abstract class Class2 implements Base {}

    • B.

      Abstract class Class2 extends Base { public boolean m1(){ return true; }}

    • C.

      Abstract class Class2 implements Base { public boolean m1(){ return (7 > 4); }}

    • D.

      Abstract class Class2 implements Base { protected boolean m1(){ return (5 > 7) }}

    Correct Answer(s)
    A. Abstract class Class2 implements Base {}
    C. Abstract class Class2 implements Base { public boolean m1(){ return (7 > 4); }}
    Explanation
    Both code fragments will compile. The first code fragment is an abstract class that implements the Base interface, which is allowed. The second code fragment is also an abstract class that implements the Base interface, and it overrides the m1() method to return a boolean value. This is also allowed.

    Rate this question:

  • 8. 

    public class Test { } What is the prototype of the default constructor?

    • A.

      Test( )

    • B.

      Public Test( )

    • C.

      Test(void)

    • D.

      Public Test(void)

    Correct Answer
    B. Public Test( )
    Explanation
    The correct answer is "public Test( )". In Java, the prototype of a constructor consists of the access modifier, the name of the class, and any parameters that the constructor takes. The default constructor is a constructor that is automatically provided by the compiler if no other constructors are defined in the class. In this case, the default constructor for the class "Test" has the access modifier "public" and does not take any parameters.

    Rate this question:

  • 9. 

    Consider the code publi class Animal { public void eat() } Which is the legal method override of Animal eat()

    • A.

      Private void eat()

    • B.

      Public void eat()

    • C.

      Public void eat() throws Exeption

    • D.

      Public String eat()

    Correct Answer
    B. Public void eat()
    Explanation
    The correct answer is "public void eat()" because it matches the method signature of the eat() method in the Animal class. In order to override a method, the method signature (including the return type, name, and parameters) must be the same as the method being overridden. The other options do not match the method signature and therefore cannot be valid method overrides.

    Rate this question:

  • 10. 

    public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } } Which of the following code fragments inserted, will allow to compile?

    • A.

      New Inner(); //At line 5

    • B.

      New Inner(); //At line 10

    • C.

      New ot.Inner(); //At line 10

    • D.

      New Outer.Inner(); //At line 10

    Correct Answer
    A. New Inner(); //At line 5
    Explanation
    The code fragment "new Inner(); //At line 5" will allow the code to compile. This is because the Inner class is a non-static inner class, and it can only be instantiated within the context of an instance of the Outer class. Since the someOuterMethod() method is within the Outer class, it can directly instantiate the Inner class using "new Inner();".

    Rate this question:

  • 11. 

    Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

    • A.

      Final

    • B.

      Private

    • C.

      Protected

    • D.

      Default

    Correct Answer
    B. Private
    Explanation
    The correct answer is private. In Java, the private access modifier is used to restrict access to a method only within the same class. This means that the method cannot be accessed or called from any other class, including subclasses. It provides the highest level of encapsulation and ensures that the method is only accessible within the class where it is defined.

    Rate this question:

  • 12. 

    Class Animal { class Dog extends Animal { class DogTest { public static void main(String ar[]) { Animal animal=new Animal(); Dog d=(Dog) animal; } } What happen when the program is executed?

    • A.

      Runtime exception

    • B.

      Throws ClassCastExeption

    • C.

      Throws NullPointerException

    • D.

      No error, will compile.

    Correct Answer
    B. Throws ClassCastExeption
    Explanation
    When the program is executed, it will throw a ClassCastException. This is because the object "animal" is an instance of the Animal class, and it is being explicitly casted to the Dog class using "(Dog) animal". However, since Animal is not a subclass of Dog, the cast is not valid and a ClassCastException is thrown.

    Rate this question:

  • 13. 

    State True or False Abstract methods must be implemented by the concrete subclass, its also like concrete subclass overrides the abstract methods 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. Abstract methods are declared in abstract classes or interfaces and do not have an implementation. Concrete subclasses are required to provide an implementation for these abstract methods, but they do not override them. Overriding is the process of providing a different implementation for a method that is already defined in the superclass.

    Rate this question:

  • 14. 

    class A { protected int method1(int a, int b) { return 0; } } Which is valid in a class that extends class A?

    • A.

      Public int method1(int a, int b) {return 0; }

    • B.

      Private int method1(int a, int b) { return 0; }

    • C.

      Public short method1(int a, int b) { return 0; }

    • D.

      Static protected int method1(int a, int b) { return 0; }

    Correct Answer
    A. Public int method1(int a, int b) {return 0; }
    Explanation
    In a class that extends class A, the method method1 can be overridden with the same access modifier or a more accessible one. The correct answer is "public int method1(int a, int b) {return 0; }" because it has the same access modifier as the original method in class A. The other options either have a different access modifier (private) or a different return type (short) which would not be valid for overriding. The last option has a different access modifier (static protected) which is more accessible than the protected access modifier in class A, so it would also be a valid override.

    Rate this question:

  • 15. 

    Which of the following class level (nonlocal) variable declarations will not compile?

    • A.

      Protected int a;

    • B.

      Transient int b = 3;

    • C.

      Private synchronized int e;

    • D.

      Volatile int d;

    Correct Answer
    C. Private synchronized int e;
    Explanation
    The private synchronized int e; declaration will not compile because synchronized is not a valid modifier for a variable. Synchronized can only be used to modify methods or blocks of code to ensure thread safety. It cannot be used to modify variables.

    Rate this question:

  • 16. 

    public class Myfile { public static void main (String[] args) { String biz = args[1]; String baz = args[2]; String rip = args[3]; System.out.println("Arg is " + rip); } } Select how you would start the program to cause it to print: Arg is 2

    • A.

      Java Myfile 222

    • B.

      Java Myfile 1 2 2 3 4

    • C.

      Java Myfile 1 3 2 2

    • D.

      Java Myfile 0 1 2 3

    Correct Answer
    C. Java Myfile 1 3 2 2
    Explanation
    The correct answer is "java Myfile 1 3 2 2". This is because in the main method, the program is accessing the elements of the args array using indexes. In this case, the value of "rip" (which is assigned to args[3]) will be "2" when the program is started with the given command.

    Rate this question:

  • 17. 

    Choose all  that apply.

    • A.

      Overloaded methods must change the argument list

    • B.

      Overloaded methods can change the argument list

    • C.

      Overloaded methods can change the return type

    • D.

      Overloaded methods must change the return type

    • E.

      Overloaded methods can delare new or broader checked exceptions

    Correct Answer(s)
    A. Overloaded methods must change the argument list
    C. Overloaded methods can change the return type
    E. Overloaded methods can delare new or broader checked exceptions
    Explanation
    Overloaded methods are multiple methods in a class that have the same name but different parameters. When overloading methods, it is necessary to change the argument list in order to differentiate between the different versions of the method. However, changing the return type is not a requirement for overloading methods. Additionally, overloaded methods have the flexibility to declare new or broader checked exceptions, but this is not a mandatory requirement.

    Rate this question:

  • 18. 

    Public class foo { public void dostuff(int y,String s) {} } class bar extends foo { public void dostuff(int y, long s)throws IOException } What is true about this?

    • A.

      Dostuff method is not overridden

    • B.

      Subclass bar overrides the dostuff() method

    • C.

      Dostuff method is not overloaded.

    • D.

      IOException an not be inserted in the overloading method.

    Correct Answer
    A. Dostuff method is not overridden
    Explanation
    The dostuff method in the subclass bar has the same name, return type, and parameters as the dostuff method in the superclass foo. However, the dostuff method in the subclass bar adds an additional checked exception, IOException, in its throws clause. This means that the dostuff method in the subclass bar is considered a different method than the dostuff method in the superclass foo. Therefore, the dostuff method is not overridden in the subclass bar.

    Rate this question:

  • 19. 

    Abstract class Director { protected String name; Director(String name) { this.name = name; } abstract void occupation(); } class FilmDirector extends Director { FilmDirector(String name) { super(name); } void occupation() { System.out.println("Director " + name + " directs films"); } } public class TestDirector { public static void main(String[] args) { FilmDirector fd = new FilmDirector("Manirathnam"); fd.occupation(); new Director("Manirathnam") { void occupation() { System.out.println("Director " + name + " also produces films"); } } .occupation(); } } Which of the following will be the output of the above code snippet?

    • A.

      Compilation fails at TestDirector class

    • B.

      Prints: Director Manirathnam also produces films

    • C.

      Prints: Director Manirathnam directs films Director Manirathnam also produces films

    • D.

      Prints: Director Manirathnam directs films

    Correct Answer
    C. Prints: Director Manirathnam directs films Director Manirathnam also produces films
    Explanation
    The code snippet creates an instance of the FilmDirector class and calls the occupation() method, which prints "Director Manirathnam directs films". Then, a new anonymous class is created based on the Director class and its occupation() method is overridden to print "Director Manirathnam also produces films". Finally, the occupation() method of this anonymous class is called, resulting in the output "Director Manirathnam also produces films".

    Rate this question:

  • 20. 

    Which of the following modifier cannot be applied to the declaration of a field?

    • A.

      Protected

    • B.

      Final

    • C.

      Abstract

    • D.

      Public

    Correct Answer
    C. Abstract
    Explanation
    The modifier "abstract" cannot be applied to the declaration of a field. The "abstract" modifier is used to indicate that a class or method is incomplete and must be implemented by a subclass. Fields, on the other hand, are concrete data members of a class and do not have the concept of being abstract. Therefore, the "abstract" modifier is not applicable to field declarations.

    Rate this question:

  • 21. 

    Consider s1 and s2 are sets. Which of the following options gives the exact meaning of the method call s1.retainAll(s2)?

    • A.

      A. transforms s1 into the union of s1 and s2

    • B.

      Transforms s1 into the intersection of s1 and s2.

    • C.

      Transforms s1 into the (asymmetric) set difference of s1 and s2

    • D.

      Copies elements from s2 to s1

    Correct Answer
    B. Transforms s1 into the intersection of s1 and s2.
    Explanation
    The method call s1.retainAll(s2) transforms s1 into the intersection of s1 and s2. This means that after the method call, s1 will only contain the elements that are present in both s1 and s2. Any elements that are not common to both sets will be removed from s1.

    Rate this question:

  • 22. 

    Class Test { Test(int i) { System.out.println("Test(" + i +")"); } } public class Question{ static Test t1 = new Test(1); Test t2 = new Test(2); static Test t3 = new Test(3); public static void main(String[] args){ Question Q = new Question(); } } Which of the following options gives the correct order of initialization?

    • A.

      A. Test(3) Test(2) Test(1)

    • B.

      Test(2) Test(1) Test(3)

    • C.

      Test(1) Test(2) Test(3)

    • D.

      Test(1) Test(3) Test(2)

    Correct Answer
    D. Test(1) Test(3) Test(2)
    Explanation
    The correct order of initialization is Test(1) Test(3) Test(2) because the static variable t1 is initialized first, followed by the instance variable t2, and then the static variable t3.

    Rate this question:

  • 23. 

    Class TestString4 { public static void main(String args[]) { String s1 = "Its Great"; String s2 = "Its Tricky"; System.out.print(s1.concat(s2).length() + " ");   System.out.print(s1.concat(s2.substring(1, s1.length())).length()); } } What will be the output of the following code snippet? Answer:

    • A.

      18 20

    • B.

      17 18

    • C.

      19 17

    • D.

      17 17

    Correct Answer
    C. 19 17
    Explanation
    The code snippet concatenates the strings "Its Great" and "Its Tricky" using the `concat()` method. It then calculates the length of the concatenated string and prints it. In the second print statement, it concatenates "Its Tricky" with a substring of "Its Great" starting from index 1 and ending at the length of "Its Great". It then calculates the length of this new concatenated string and prints it. Therefore, the output will be "19 17".

    Rate this question:

  • 24. 

    Which of the following options is true about multi-level inheritance?

    • A.

      Inheriting from two super classes

    • B.

      Inheriting from a class which is already in an inheritance hierarchy

    • C.

      Inheriting from more than one super class

    • D.

      Inheriting from a single class

    Correct Answer
    B. Inheriting from a class which is already in an inheritance hierarchy
    Explanation
    Multi-level inheritance refers to a situation where a class inherits from a class that is already in an inheritance hierarchy. This means that the derived class not only inherits the properties and methods of its immediate superclass but also inherits the properties and methods of the superclass's superclass, and so on. In other words, there are multiple levels of inheritance involved.

    Rate this question:

  • 25. 

    Anonymous class can have their own members.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Anonymous classes in programming languages, such as Java, can indeed have their own members. These members can include fields, methods, and even constructors. This allows anonymous classes to have their own unique behavior and functionality, separate from other classes. This feature is particularly useful when creating small, one-time use classes that do not need to be explicitly named or reused elsewhere in the code.

    Rate this question:

  • 26. 

    Interface A { public int getValue(); } class B implements A { public int getValue() { return 1; } }   class C extends B { // insert code here } Which of the following code fragments, when inserted individually at the commented line (// insert code here), makes use of polymorphism? (Choose all that apply)

    • A.

      Public void add(A a) { a.getValue(); }

    • B.

      Public void add(B b) { b.getValue(); }

    • C.

      Public void add(C c1, C c2) { c1.getValue(); }

    • D.

      Public void add(C c) { c.getValue(); }

    • E.

      Public void add(A a, B b) { a.getValue(); }

    Correct Answer(s)
    A. Public void add(A a) { a.getValue(); }
    B. Public void add(B b) { b.getValue(); }
    E. Public void add(A a, B b) { a.getValue(); }
    Explanation
    The correct answer is "public void add(A a) { a.getValue(); },public void add(B b) { b.getValue(); },public void add(A a, B b) { a.getValue(); }".

    These code fragments make use of polymorphism because they are accepting objects of different types (A, B, A and B) as parameters, but they are all implementing the same interface (A). This allows for different objects to be passed into the methods, but they can all be treated as objects of type A due to polymorphism. The getValue() method is then called on the objects, which will be dynamically bound at runtime based on the actual type of the object.

    Rate this question:

  • 27. 

    Consider the following code:   public class Code9 { public static void main(String args[]) { System.out.println(Math.abs(Integer.MIN_VALUE)); } } Which of the following will be the output for the above given program? (Choose 2)

    • A.

      Compilation Error

    • B.

      Compiles successfully and prints a value which is less than zero

    • C.

      Compiles successfully and prints a value which is equal to Integer.MIN_VALUE

    • D.

      Compiles successfully and prints a value which is equal to -Integer.MAX_VALUE

    • E.

      Compiles successfully and prints a value which is equal to Integer.MIN_VALUE + 1

    Correct Answer(s)
    B. Compiles successfully and prints a value which is less than zero
    C. Compiles successfully and prints a value which is equal to Integer.MIN_VALUE
    Explanation
    The code compiles successfully and prints a value which is less than zero because Math.abs(Integer.MIN_VALUE) returns a negative value. It also prints a value which is equal to Integer.MIN_VALUE because Math.abs() returns the absolute value of the given integer, and the absolute value of Integer.MIN_VALUE is still Integer.MIN_VALUE.

    Rate this question:

  • 28. 

    Which of the following options are true about Associations?(choose 2)

    • A.

      Association refers to a class reuses the properties and methods of another class

    • B.

      Association refers to an object composed of set of other objects

    • C.

      Association refers to binding of related data and behaviours into a single entity

    • D.

      Associations are bi-directional

    • E.

      In Associations, cardinality refers to the number of related objects

    Correct Answer(s)
    D. Associations are bi-directional
    E. In Associations, cardinality refers to the number of related objects
    Explanation
    Associations are bi-directional means that the relationship between two classes can be navigated in both directions. In other words, if class A is associated with class B, then class B is also associated with class A.

    In Associations, cardinality refers to the number of related objects means that the cardinality of an association specifies how many objects of one class are associated with how many objects of another class. It defines the number of instances that participate in the association.

    Rate this question:

  • 29. 

    State which of the following are default delimiters?(Choose 3)

    • A.

      Form feed character

    • B.

      Tab character

    • C.

      The dollar character

    • D.

      Space character

    • E.

      The underscore character

    Correct Answer(s)
    A. Form feed character
    B. Tab character
    D. Space character
    Explanation
    The form feed character, tab character, and space character are default delimiters. These characters are used to separate or delimit different elements or values in a given context. The form feed character is used to indicate a new page or section, the tab character is used to create horizontal spacing, and the space character is used to separate words or elements.

    Rate this question:

  • 30. 

    Consider the following code: package com.java.test; public class A { public void m1() { System.out.print("A.m1, ");} protected void m2() { System.out.print("A.m2, ");} private void m3() {System.out.print("A.m3, ");} void m4() {System.out.print("A.m4, ");} } class B { public static void main(String[] args) { A a = new A(); a.m1();// 1 a.m2(); // 2 a.m3(); // 3 a.m4(); // 4 } } Assume that both of the above classes are stored in a single source file called 'A.java'. Which of the following gives the valid output of the above code?

    • A.

      Prints: A.m1, A.m2, A.m3, A.m4,

    • B.

      Compile-time error at 4.

    • C.

      Compile-time error at 3.

    • D.

      Compile-time error at 2.

    Correct Answer
    C. Compile-time error at 3.
    Explanation
    The code will give a compile-time error at 3 because the method m3() in class A is declared as private. Private methods can only be accessed within the same class and cannot be accessed by other classes, even if they are in the same source file. Therefore, when trying to call the m3() method from class B, a compile-time error will occur.

    Rate this question:

  • 31. 

    Constructors can be inherited and overriddden .

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Constructors cannot be inherited or overridden. In object-oriented programming, a constructor is a special method used to initialize objects of a class. It is automatically called when an object is created. Inheritance allows a subclass to inherit the properties and methods of its superclass, but constructors are not inherited. Each class must have its own constructor to initialize its specific properties. Similarly, constructors cannot be overridden because they are not inherited. Subclasses can have their own constructors, but they cannot override the constructor of the superclass. Therefore, the given statement is false.

    Rate this question:

  • 32. 

    Class a { a(){ this("foo");} a(String s) {this(); } What happens when the program runs.

    • A.

      Runtime error

    • B.

      Throw an Stackoverflowerror exception

    • C.

      Compilation fails

    • D.

      Syntax error.

    Correct Answer
    B. Throw an Stackoverflowerror exception
    Explanation
    The program will throw a StackOverflowError exception. This is because the constructor "a()" calls another constructor "a(String s)", which in turn calls the first constructor again. This creates an infinite loop of constructor calls, eventually causing the stack to overflow and resulting in a StackOverflowError exception.

    Rate this question:

  • 33. 

    Package com.packageone; public class A { private int x; public A(int x) { this(); this.x = x; } abstract void print(); } And class B is defined in packagetwo as follows: package com.packagetwo; import com.packageone.A; class B extends A { B(int x) { super(x); } void print() { System.out.println(x); } } class C { public static void main(String args[]) { A a = new B(10); } } Which of the following changes will make the code to run properly? (Choose 3)

    • A.

      Class B should be declared public abstract

    • B.

      Class A should be declared public abstract

    • C.

      The print method in class B should refer the x as super.x

    • D.

      The member x in class A should be declared as protected

    • E.

      This() method call should be removed from the class A constructor

    Correct Answer(s)
    B. Class A should be declared public abstract
    D. The member x in class A should be declared as protected
    E. This() method call should be removed from the class A constructor
    Explanation
    The code will run properly if the following changes are made:
    1. class A should be declared public abstract - This is because class B extends class A, and in Java, a subclass cannot extend a class that is not accessible.
    2. The member x in class A should be declared as protected - This is because class B needs to access the member x in order to print its value.
    3. this() method call should be removed from the class A constructor - This is because the constructor in class A is calling itself, resulting in an infinite loop. Removing this call will prevent the infinite loop and allow the code to run properly.

    Rate this question:

  • 34. 

    Choose statements that are iilegal.

    • A.

      The first line in the constructor must be call to super()

    • B.

      This() always means call to another constructor in another class

    • C.

      Super() always means call to another constructor in another class

    • D.

      There is no difference in using this() and super().

    Correct Answer(s)
    B. This() always means call to another constructor in another class
    D. There is no difference in using this() and super().
    Explanation
    The first statement "The first line in the constructor must be call to super()" is incorrect. While it is common practice to call the superclass constructor as the first line in a subclass constructor using the "super()" keyword, it is not mandatory. The order of statements in a constructor can vary as long as the superclass constructor is eventually called.

    The second statement "this() always means call to another constructor in another class" is correct. The "this()" keyword is used to invoke another constructor within the same class.

    The third statement "super() always means call to another constructor in another class" is incorrect. The "super()" keyword is used to invoke a constructor in the superclass, not necessarily in another class.

    The fourth statement "There is no difference in using this() and super()" is incorrect. "this()" is used to refer to the current instance of a class, while "super()" is used to refer to the superclass. They have different purposes and cannot be used interchangeably.

    Rate this question:

  • 35. 

    Consider the following code: package test; class Target { String name = "hello"; } Which of the following options are valid that can directly access and change the value of the variable 'name' in the above code? (Choose 2)

    • A.

      Any class in the test package

    • B.

      Any class that extends Target outside the test package

    • C.

      Any class

    • D.

      Only the Target class

    • E.

      Any class that extends Target within the test package

    Correct Answer(s)
    A. Any class in the test package
    E. Any class that extends Target within the test package
    Explanation
    Any class in the test package can directly access and change the value of the variable 'name' because it is declared with default access modifier, which means it is accessible within the same package. Additionally, any class that extends Target within the test package can also access and change the value of the variable 'name' as it inherits the variable from the superclass.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jun 04, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 16, 2011
    Quiz Created by
    Abiprakasam
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.