Alien head

Java OOPs Quiz - SCJP

Description: Java OOPs Quiz - SCJP
Number of Questions: 10
Created by:
Tags: java scjp

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.

Answer: 4

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.

Answer: 1

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.

Answer: 1,4

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); }

Answer: 3,4

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

Answer: 4

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.

Answer: 1,4

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.

Answer: 3

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.

Answer: 3

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.

Answer: 1

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 */ }

Answer: 2,3,5
+ View questions