0

Java OOPs Quiz - SCJP

Description: Java OOPs Quiz - SCJP
Number of Questions: 10
Created by:
Tags: java scjp
Attempted 0/10 Correct 0 Score 0

Which statement is true for the following code?

public class Plant {
   private String name;
   public Plant(String name) { this.name = name; }
   public String getName() { return name; }
}

public class Tree extends Plant {
   public void growFruit() { }
   public void dropLeaves() { }
}
  1. The code will compile without changes.

  2. The code will compile if public Tree() { Plant(); } is added to the Tree class.

  3. The code will compile if public Plant() { Tree(); } is added to the Plant class.

  4. The code will compile if public Plant() { this("fern"); } is added to the Plant class.


Correct Option: D
Explanation:

This code defines two classes, Plant and Tree. Tree extends Plant, meaning that it inherits all of the fields and methods of Plant.

The Plant class has a single constructor that takes a String argument and sets the name field to that value. It also has a getter method for the name field.

The Tree class has two methods, growFruit() and dropLeaves(), but no constructors.

Option A) The code will compile without changes. - This is incorrect because the Tree class does not have a constructor, and it needs to call the constructor of the parent class when an instance of Tree is created.

Option B) The code will compile if public Tree() { Plant(); } is added to the Tree class. - This is incorrect because the correct syntax to call the parent constructor is super(), not the name of the parent class.

Option C) The code will compile if public Plant() { Tree(); } is added to the Plant class. - This is incorrect because it does not make sense to call the constructor of a subclass from the constructor of the parent class.

Option D) The code will compile if public Plant() { this("fern"); } is added to the Plant class. - This is correct because it adds a new constructor to the Plant class that takes no arguments and calls another constructor of the same class with a default argument. This allows the Tree class to inherit this constructor and call it with the default argument when an instance of Tree is created.

Therefore, the correct answer is D.

What will the result for following code?

public class Yikes {
    public static void go(Long n) {System.out.println("Long ");}
    public static void go(Short n) {System.out.println("Short ");}
    public static void go(int n) {System.out.println("int ");}

    public static void main(String [] args) {
        short y = 6;
        long z = 7;
        go(y);
        go(z);
    }
}
  1. int Long

  2. Short Long

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: A
Explanation:

To solve this question, the user needs to know about method overloading, widening, and boxing/unboxing conversions in Java.

Each option can be evaluated as follows:

Option A: int Long

  • The first call to go() passes in a short argument. Since there is no exact match for a short parameter, the short value is widened to an int. Thus, the first call to go() invokes the go(int n) method and the output is "int ".
  • The second call to go() passes in a long argument, which matches the go(Long n) method. Thus, the second call to go() invokes the go(Long n) method and the output is "Long ".
  • Therefore, the output of the program is "int Long".

Option B: Short Long

  • The first call to go() passes in a short argument, which matches the go(Short n) method. Thus, the first call to go() invokes the go(Short n) method and the output is "Short ".
  • The second call to go() passes in a long argument, which matches the go(Long n) method. Thus, the second call to go() invokes the go(Long n) method and the output is "Long ".
  • Therefore, the output of the program is "Short Long".

Option C: Compilation fails.

  • There are no compilation errors in the code. The program will compile and run without errors.
  • Therefore, option C is incorrect.

Option D: An exception is thrown at runtime.

  • There are no exceptions thrown in the code. The program will execute without throwing any exceptions.
  • Therefore, option D is incorrect.

Therefore, the correct answer is:

The Answer is: A. int Long

Which two statements are true about has-a and is-a relationships?

  1. Inheritance represents an is-a relationship.

  2. Inheritance represents a has-a relationship.

  3. Interfaces must be used when creating a has-a relationship.

  4. Instance variables can be used when creating a has-a relationship.


Correct Option: A,D
Explanation:

To understand the differences between has-a and is-a relationships, let's first define them:

  • Has-a relationship: This is a composition relationship between two classes where one class contains an instance of the other class as a member variable. It represents a "part-of" relationship.
  • Is-a relationship: This is an inheritance relationship between two classes where one class is a subclass of the other class. It represents a "kind-of" relationship.

Now, let's go through each option to understand why it is correct or incorrect:

Option A) Inheritance represents an is-a relationship - This option is true because inheritance is used to create a subclass that is a specialized version of the superclass. The subclass "is-a" kind of the superclass.

Option B) Inheritance represents a has-a relationship - This option is incorrect because inheritance does not involve composition between two classes. It is used to create a specialized version of an existing class.

Option C) Interfaces must be used when creating a has-a relationship - This option is incorrect because interfaces are not required to create a has-a relationship. A has-a relationship can be created using instance variables.

Option D) Instance variables can be used when creating a has-a relationship - This option is true because instance variables can be used to represent the composition relationship between two classes. One class can contain an instance of the other class as a member variable.

Therefore, the correct options are A and D.

Which two, independently, will allow Sub to compile?

1. class Super {
2.   private int a;
3.   protected Super(int a) { this.a = a; }
4. }
...
11. class Sub extends Super {
12.   public Sub(int a) { super(a); }
13.   public Sub() { this.a = 5; }
14. }
  1. Change line 2 to: public int a;

  2. Change line 2 to :protected int a;

  3. Change line 13 to :public Sub() { this(5); }

  4. Change line 13 to :public Sub() { super(5); }


Correct Option: C,D
Explanation:

To solve this question, the user needs to know about access modifiers, constructors, and inheritance in Java.

Line 2 declares a private instance variable a in class Super, which cannot be accessed from the subclass Sub. Line 3 is a constructor that initializes the value of a. Line 11 declares a subclass Sub that inherits from Super.

Now, let's go through each option and explain why it is right or wrong:

A. Change line 2 to: public int a; This option is incorrect because changing the access modifier of a to public would make it accessible from anywhere, including the subclass Sub. However, the a variable is still not visible in Sub, because it is declared in the superclass as private. Therefore, this option will not allow Sub to compile.

B. Change line 2 to: protected int a; This option is correct because changing the access modifier of a to protected would make it accessible from the subclass Sub. Since a is now declared as protected in the superclass, it can be accessed from the subclass. Therefore, this option will allow Sub to compile.

C. Change line 13 to: public Sub() { this(5); } This option is incorrect because changing the no-argument constructor to call another constructor with an argument using this would not compile since the superclass constructor is expecting an argument. Therefore, this option will not allow Sub to compile.

D. Change line 13 to: public Sub() { super(5); } This option is correct because changing the no-argument constructor to call the superclass constructor that takes an argument will initialize the a variable in the superclass with the value of 5. Therefore, this option will allow Sub to compile.

Therefore, the correct answer is:

The Answer is: D

What will be the output?

public class Base {
  public static final String FOO = "foo";
  public static void main(String[] args) {
    Base b = new Base();
    Sub s = new Sub();
    System.out.print(Base.FOO);
    System.out.print(Sub.FOO);
    System.out.print(b.FOO);
    System.out.print(s.FOO);
    System.out.print(((Base)s).FOO);
  } 
}

class Sub extends Base {
    public static final String FOO="bar";
}

  1. foofoofoofoofoo

  2. foobarfoobarbar

  3. foobarfoofoofoo

  4. foobarfoobarfoo

  5. barbarbarbarbar


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) foofoofoofoofoo - This option is incorrect. The output will not consist of repeated "foo" values throughout.

Option B) foobarfoobarbar - This option is incorrect. The output will not consist of repeated "foobar" values throughout.

Option C) foobarfoofoofoo - This option is incorrect. The output will not consist of the sequence "foobar" followed by "foo" repeated three times.

Option D) foobarfoobarfoo - This option is correct. The output will consist of the sequence "foobar" followed by "foobar" again, and then "foo".

Option E) barbarbarbarbar - This option is incorrect. The output will not consist of repeated "bar" values throughout.

The correct answer is Option D. This option is correct because the output will be "foobarfoobarfoo".

Explanation:

  • The Base.FOO will print "foo" since it is a static final variable in the Base class.
  • The Sub.FOO will print "bar" since it is a static final variable in the Sub class.
  • The b.FOO will print "foo" since it is accessing the FOO variable from the Base class using an instance of the Base class.
  • The s.FOO will print "bar" since it is accessing the FOO variable from the Sub class using an instance of the Sub class.
  • The ((Base)s).FOO will print "foo" since it is explicitly casting the Sub instance s to a Base instance and accessing the FOO variable from the Base class.

Therefore, the output will be "foobarfoobarfoo".

Please select the most appropriate option.

Which two statements are true?

  1. An encapsulated, public class promotes re-use.

  2. Classes that share the same interface are always tightly encapsulated.

  3. An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods.

  4. An encapsulated class allows a programmer to change an implementation without affecting outside code.


Correct Option: A,D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) An encapsulated, public class promotes re-use - This option is true. Encapsulation refers to the practice of hiding internal implementation details of a class and providing a public interface for interacting with the class. By encapsulating the internal details, the class becomes more modular and can be easily reused in different parts of a program.

Option B) Classes that share the same interface are always tightly encapsulated - This option is incorrect. Two classes that share the same interface may or may not be tightly encapsulated. Encapsulation is about hiding implementation details, while an interface defines a contract for interacting with a class. It is possible for multiple classes to implement the same interface but have different levels of encapsulation.

Option C) An encapsulated class allows subclasses to overload methods, but does NOT allow overriding methods - This option is incorrect. Encapsulation does not restrict the ability to override methods. Overloading refers to defining multiple methods with the same name but different parameters, while overriding refers to redefining a method in a subclass with the same name and parameters as the method in the superclass. Encapsulation is unrelated to these concepts.

Option D) An encapsulated class allows a programmer to change an implementation without affecting outside code - This option is true. Encapsulation allows the internal implementation details of a class to be hidden from outside code. This means that the class can be modified or refactored internally without affecting the code that interacts with the class. As long as the public interface remains the same, the outside code will not be affected.

The correct answers are A) An encapsulated, public class promotes re-use and D) An encapsulated class allows a programmer to change an implementation without affecting outside code. These options are true because encapsulation promotes reusability and allows for changes in implementation without affecting the code that interacts with the class.

What is the output?

public class Wow {
 public static void go(short n) {System.out.println("short");}
 public static void go(Short n) {System.out.println("SHORT");}
 public static void go(Long n) {System.out.println(" LONG");}
 public static void main(String [] args) {
   Short y = 6;
   int z = 7;
   go(y);
   go(z);
 }
}
  1. short LONG

  2. SHORT LONG

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) short LONG - This option is incorrect because the expected output is "Compilation fails" (as mentioned in the correct answer). Option B) SHORT LONG - This option is incorrect because the expected output is "Compilation fails" (as mentioned in the correct answer). Option C) Compilation fails - This option is correct. The reason for this is that there are multiple overloaded methods with similar parameter types (short, Short, and Long). When the method is called with the variable 'y', which is of type Short, it is ambiguous whether it should call the method go(short n) or go(Short n) because both are valid options. This ambiguity results in a compilation error. Option D) An exception is thrown at runtime - This option is incorrect because the issue here is a compilation error, not a runtime exception.

The correct answer is C. Compilation fails.

Please let me know if I can help you with anything else.

Which statement is true

public class CreditCard {

 private String cardID;
 private Integer limit;
 public String ownerName;

 public void setCardInformation(String cardID,
 String ownerName,
 Integer limit) {
  this.cardID = cardID;
  this.ownerName = ownerName;
  this.limit = limit;
 }
}
  1. The class is fully encapsulated.

  2. The code demonstrates polymorphism.

  3. The ownerName variable breaks encapsulation.

  4. The cardID and limit variables break polymorphism.

  5. The setCardInformation method breaks encapsulation.


Correct Option: C
Explanation:

To solve this question, the user needs to have an understanding of object-oriented programming concepts such as encapsulation and polymorphism.

Now, let's go through each statement and evaluate its correctness:

A. The class is fully encapsulated: This statement is incorrect. Encapsulation refers to the practice of keeping the internal state and implementation details of an object hidden and accessible only through defined methods. In this case, the class has private instance variables (cardID and limit) and a public method (setCardInformation) to modify their values. However, the class also has a public instance variable (ownerName) that can be accessed directly, which breaks encapsulation.

B. The code demonstrates polymorphism: This statement is incorrect. Polymorphism refers to the ability of objects of different classes to be treated as objects of a common parent class. The code provided does not demonstrate polymorphism.

C. The ownerName variable breaks encapsulation: This statement is correct. The ownerName variable is declared as public, which means it can be accessed and modified directly from outside the class, breaking encapsulation.

D. The cardID and limit variables break polymorphism: This statement is incorrect. The cardID and limit variables have no relation to polymorphism. Polymorphism involves the ability to treat objects of different classes as objects of a common parent class.

E. The setCardInformation method breaks encapsulation: This statement is incorrect. The setCardInformation method is designed to modify the values of the private instance variables (cardID, ownerName, limit), which is a common practice in encapsulation.

The Answer is: C

What is the output?

interface TestA { String toString(); }
 public class Test {
 public static void main(String[] args) {
  System.out.println(new TestA() {
  public String toString() { return "test"; }
 });
 }
}
  1. test

  2. null

  3. Compilation fails because of an error in line 1.

  4. An exception is thrown at runtime.


Correct Option: A
Explanation:

To solve this question, the user needs to understand Java syntax, specifically the concepts of interfaces, anonymous inner classes, and the toString() method.

In the given code, an anonymous inner class is created that implements the TestA interface. This inner class overrides the toString() method from the Object class and returns the string "test".

Now let's go through each option and explain why it is right or wrong:

A. test: This option is correct. When the toString() method is called on the anonymous inner class object, it returns the string "test". This is the output that will be printed to the console.

B. null: This option is incorrect. The toString() method is overridden in the anonymous inner class to return the string "test", not null. Therefore, the output will not be null.

C. Compilation fails because of an error in line 1: This option is incorrect. There is no error in line 1. The interface TestA is defined correctly.

D. An exception is thrown at runtime: This option is incorrect. There is no exception thrown in the given code. The code will compile and run without any exceptions.

The Answer is: A. test

Which three methods, inserted individually at line 14, will correctly complete class Two?

class One {
 void foo() { }
}
class Two extends One {
 //insert method here
}
  1. int foo() { /* more code here */ }

  2. void foo() { /* more code here */ }

  3. public void foo() { /* more code here */ }

  4. private void foo() { /* more code here */ }

  5. protected void foo() { /* more code here */ }


Correct Option: B,C,E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) int foo() { /* more code here */ } This option is incorrect because it changes the return type of the method foo() from void to int, which is not compatible with the return type in the parent class.

Option B) void foo() { /* more code here */ } This option is correct because it overrides the foo() method in the parent class with the same return type (void). The method in the subclass can have additional code to extend the functionality.

Option C) public void foo() { /* more code here */ } This option is correct because it overrides the foo() method in the parent class with the same return type (void) and adds the public access modifier. The access modifier can be the same or more accessible than the method in the parent class.

Option D) private void foo() { /* more code here */ } This option is incorrect because it changes the access modifier of the method foo() from the default (package-private) to private. Subclasses cannot have greater restrictions on the access modifier than the parent class.

Option E) protected void foo() { /* more code here */ } This option is correct because it overrides the foo() method in the parent class with the same return type (void) and adds the protected access modifier. The access modifier can be the same or more accessible than the method in the parent class.

The correct answer is B, C, E. These options correctly complete class Two by overriding the foo() method from the parent class with the same return type (void) and adding the appropriate access modifiers.

Please select the most appropriate option.

- Hide questions