Java certification Quiz
Java Quiz - 29

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

Short s1 = 100;
Integer s2 = 200;
Long s3 = s1+(long)s2;
System.out.println(s3);
a) Compile error
b) Exception thrown
c) 200
d) 300
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) 300 : s2 int is cast to long, resolving type issue
Correct!
300 : s2 int is cast to long, resolving type issue

2. What is the output?

int var = 9;
if(var++ <10) {
  System.out.println(var++ +" Less");
}
else {
 System.out.println(var++ +" More");
}
a) 10 More
b) 10 Less
c) 11 More
d) 11 Less

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) 10 Less : ++ operator works after usage of variable
Correct!
10 Less : ++ operator works after usage of variable

3. What is the output?

int[] n1 = new int[3];
int[] n2 = {1,2,3,4,5};
n1=n2;
for (int i : n1) {
  System.out.print(i+":");
}
a) Compile error
b) Exception thrown
c) 1:2:3:4:5:
d) 1:2:3:

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) 1:2:3:4:5:
Correct! 1:2:3:4:5:

4. What changes can make code compile?

1. class P {
2.    public void print() {
3.        throw new IOException();
4.    }
5. }
6. class Test {
7.   public static void main(String[] args) {
8.       P p = new P();
9.       p.print();
10.   }
11.}
A. add throws IOException at line 2
B. add throws IOException at line 7
C. try/catch at line 9
D. add throws Exception at line 2
a) A or B with C
b) A with B or C
c) B with C
d) A or D

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) A with B or C : Checked exceptions have to be caught or declared in throws clause
Correct! A with B or C
Checked exceptions have to be caught or declared in throws clause

5. Which lines do not compile?

1.    int i = 25;
2.    float f = 10.3f;
3.    double d = 20.1;
4.    i = f;
5.    f= i;
6.    d= f;
7.    f = d;
8.    d= i;
9.    i = d;
a) 4, 7, 9
b) 7, 9
c) 4, 5, 7, 9
d) 5, 7, 9

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) 4, 7, 9 : int can be assigned to float and both to double
Correct! 4, 7, 9
int can be assigned to float and both to double

6. What should be added at XX and YY to get ouput 235?

 int[] arr = {1,2,3,4,5,6};
 for (int i=0;i<arr.length;i++) {
   if(i<2) {
     XX
   }
   System.out.print(i);
   if(i==3) {
     YY
   }
 }
a) continue and break
b) continue and continue
c) continue and i++
d) continue at XX only
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) continue and i++ : continue abandons current cycle of the loop and goes to next iteration
Correct!
continue and i++ : continue abandons current cycle of the loop and goes to next iteration

7. What is the output?

 String [][] arr = new String[2][];
 arr[0] = new String[2];
 arr[1] = new String[3];
 int i = 85;
 for (int j = 0; j < arr.length; j++) {
   for (int k = 0; k < arr.length; k++) {
     arr[j][k] = ""+i;
     i++;
   }
 }
 for(String[] a :arr) {
   for(String b:a) {
     System.out.print(b+" ");
   }
 }
a) 85 86 87 88 89
b) 85 86 87 88
c) 85 87 86 88 null
d) 85 86 87 88 null
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) 85 86 87 88 null : inner loop runs twice only
Correct!
85 86 87 88 null : inner loop runs twice only