Java certification Quiz
Java Quiz - 9

« 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 »

1. What is the output if Main is run?
public abstract class Vehicle {
 private int tyres;
 public void setTyres(int tyres) {
   this.tyres = tyres;
 } 
 public int getTyres() {
   return tyres;
 } 
}
public class Car extends Vehicle {
  @Override
  public int getTyre() {
     return super.getTyres()+1;
  } 
}
public class Main {
  public static void main(String args[]) {
    Car c = new Car(); 
    c.setTyres(5); 
    System.out.println("Tyres = "+c.getTyres()); 
  }
}
a) Tyres = 5
b) Compile error
c) Tyres = 6
d) Runtime Exception
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Compile error. The annotation @Override is for compile time safety. Method getTyre() is mis-spelt in class Car : should be getTyres().
Correct!
The annotation @Override is for compile time safety. Method getTyre() is mis-spelt in class Car : should be getTyres()
2. What is the output?
import static java.lang.Math.*;
public class Main {
   double rad = 0.0;
   Main(){
      rad = cos(PI * 45); 
   }
   public double getRad(){
      return rad;
   }
  public static void main(String args[]){
      Main m = new Main();
      System.out.println("r = "+m.getRad()); 
  }
}
a) Compile error. Method cos() unknown
b) Run time exception.
c) r = -1.0
d) Compile error. import static not allowed

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) r = -1.0 A static import ensures all static members are imported into the class and can be used directly
Correct!
A static import ensures all static members are imported into the class and can be used directly
3. Which of these demonstrate encapsulation of data?
  A. Member data have no access modifiers.
  B. Member data can be modified directly.
  C. The access modifier for methods is protected.
  D. The access modifier to member data is private.
  E. Methods provide for access and modification of data.
a) A & C
b) C & E
c) D & E
d) B & E

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) D & E. They demonstrate encapsulation of data
Correct!
D & E demonstrate encapsulation of data
4. For the following :
 public class B extends A{
    public static void main (String [] args) {
        B b = new B();
        System.out.println(b);
    }
    public String toStringObject() {
       return super.toString() +"45";
    }
 }

class A {
    public String toString() {
       return "89";
    }
} 
a) Ouput looks like : B@15b7986
b) Output is : 8945
c) Output is : 89
d) Compile Error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) 89 : toString() of super is called
Correct!
89 : toString() of super is called
5. 
 public class OverLoadTest {
    private int m, n;
    private float k;
    public void setNum(int x, int y, float z){
       m = x;
       n = y;
       k = z;
   }
 }
Which of the following overloads setNum() method in above class?
A. private void setNum(int x,int y,float z){
       m = x;
       n = y;
       k = z;
   }
B. public void setNum(int x,float z,int y){
           setNum(x,y,z);
   }
C. public void setNum(int x,float z,int y){
         this(x,y,z);
   }
D. public void setNum(int x,float y){
       m = x;
       k = y;
   }
E. public void setNum(int xm,int yn,float zk){
       m =xm;
       n =yn;
       k =zk;
  }
a) D & E.
b) B & D.
c) A & D
d) B & E

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) B & D. Over loading is implemented by same name but different parameters or their order
Correct! B & D. Over loading is implemented by same name but different parameters or their order
6. Which interface does java.util.Hashtable implement?
a) java.util.Map
b) java.util.List
c) java.util.Hashable
d) java.util.Collection

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Hashtable implements java.util.Map
Correct!
Hashtable implements java.util.Map
7. What is the correct statement at XX for an inner class?
 public class MyImp {
    public class InsideOne {
      public void getMe() {
        System.out.println("inside InsideOne");
      }
   }
 }
 public class Test {
   public static void main (String [] args) {
     MyImp mi = new MyImp();
     XX 
     mio.getMe();
   }
 }
a) MyImp.InsideOne mio = new mi.InsideOne();
b) MyImp mio = mi.new InsideOne();
c) MyImp.InsideOne mio = mi.new InsideOne();
d) MyImp mio = new mi.InsideOne();

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) MyImp.InsideOne mio = mi.new InsideOne();
Correct!
MyImp.InsideOne mio = mi.new InsideOne();