Java certification Quiz
Java Quiz - 1

Total Questions : 222

« 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 of the following :

1  class StringCheck {
2    static String[] str=new String[50];
3    public static void main(String[] args) {
4        for(int i=0; i<str.length; i++) {
5           str[i] = i;
6           System.out.print(str[i]);
7        }
8    }
9 }
a) compile error at line 5.
b) 0 to 49 printed continuously.
c) compile error at line 6.
d) null printed 50 times.
Wrong! Try again.
Wrong Again!Try one more chance.
Wrong!
The correct answer is a) compile error at line 5.
str[i] is String type array, cannot assign int to it.
Correct!
str[i] is String type array, cannot assign int to it.

2. What is the output of the following :

  1   class StringCheck {
  2     static String[] str=new String[50];
  3     public static void main(String[] args) {
  4	for(int i=0; i<str.length; i++) {
  5	System.out.print(str[i]);
  6	}
  7     }
  8   }
a) compile error at line 5.
b) 0 to 49 printed continuously.
c) runtime error.
d) null printed 50 times.
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) null printed 50 times.
String array is assigned null by default.
Correct!
null printed 50 times. String array is assigned null by default.

3. For the code below :

  class StringCheck {
     public static void main(String[] args) {
         String str = null;
         System.out.print(str);
     }
  }
a) Cannot access str without initialising.
b) NullPointerException thrown.
c) null is printed as output.
d) prints blank space
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) null is printed as output.
Correct!
null is printed as output.

4. For the code below :

public class Test {
    public void doIt() {
       if(this == null)
          System.out.println("this is null");
       else
          System.out.println("this is not null");
    }
     public static void main (String [] args) {
           Test t = new Test();
            t.doIt();
     }
}
a) Compile error : this can never be null
b) Gives output: this is null
c) Gives output: this is not null
d) Runtime exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Gives output: this is not null
Correct!
Gives output: this is not null

5. Java implements polymorphism by :

   1. keyword extends
   2. function overriding
   3. function overloading
   4. keyword implements
a) 1 and 4
b) 2 and 3
c) None of them
d) All of them

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) overriding and overloading ensures multiple behaviour, thus implementing polymorphism
Correct!
overriding and overloading ensures multiple behaviour, thus implementing polymorphism

6. What is the output for the foll :

   public class Test {
      public static void main(String [] args) {
         short a, b, c=0;
         a=1;
         b=2;
         c=a+b;
         System.out.println(c);
      }
  }
a) Compile error.
b) 3
c) 3.0
d) Runtime exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Compile error.
In the expression c=a+b, a and b are converted to int. Casting can solve the issue: c=(short)(a+b);
Correct!
In the expression c=a+b, a and b are converted to int. Casting can solve the issue: c=(short)(a+b);

7. What is the output :

import java.io.IOException;
public class Test {
     public Test () {
         try {
           m1();
           System.out.println("No error");
         }
         catch (IOException e) {
            System.out.println("IOException caught");
         }
         catch (Exception e) {
            System.out.println("Exception caught");
         }
     }
     public void m1() {
        throw new IOException();
     }
     public static void main (String [] args) {
       Test t = new Test();
     }
}
a) Compile error
b) IOException caught
c) Exception caught
d) No error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Compile error.
IOException must be declared to be thrown in m1()
Correct!
IOException must be declared to be thrown in m1()
J2EE Quiz