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

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
  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

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

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

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

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

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
  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
- Hide questions