JAVA QUIZ 10

10
Created on By Sirjana GhimireAcharya

JAVA QUIZ 10

1 / 10

  1. Predict the output of the following program.

public class Example {

public static void main(String[] args) {
int x =0;

int[] nums= {1,2,3,5};

for (int i:nums)
switch(i){
case 1:
x += i;
case 5:
x += i;
default:
x += i;
case 2:
x += i;
}
System.out.println(x);
}

}

2 / 10

2. What will be output of following program ?

public class Test{

private static int increment(int i){
int num= (++i) + (i++);
return num;
}

public static void main(String[] args) {

for(int i=0; i < 5; i = increment(i)){
System.out.print(i);
}
}

}

3 / 10

3. What will be output of following program ?

public class Demo {

static String str;

public static boolean test1(){
return new Boolean("1");
}

public static boolean test2(){
return new Boolean(str);
}

public static void main(String[] args) {

if(test1())
System.out.print("1");
if(!test2())
System.out.print("2");
if(test1() != test2())
System.out.print("3");
}

}

4 / 10

4. What will be output of following program ?

public class Base {
private static int increment(int i){
int num= (++i) + (i++);
return num;
}

public static void main(String[] args) {

for(int i=0; i < 5; increment(i)){
System.out.print(i);
}
}

}

5 / 10

5. What will happen when you compile and run the following code?

public class Example {
private static final int ON = 1;
private static final int OFF = 0;

public static void main(String[] args){

int state = 1;
switch(state){

case ON:
System.out.println("On");
break;

default:
System.out.println("Unknown");

case OFF:
System.out.println("Off");

}
}

}

6 / 10

6. What will happen when you compile and run the following code?

public class Test{
public static void main(String[] args){

final int ON = 1;
final int OFF = 0;

int state = 3;

switch(state){
case ON:
System.out.print("On");
break;

default:
System.out.print("Unknown");

case OFF:
System.out.print("Off");
}
}
}

7 / 10

7. What will happen when you compile and run the following code?

public class Example {
static char BLGR_A = 'a';
static char BLGR_B = 'b';
static char BLGR_O = 'o';

public static void main(String[] args) {

char bloodgroup = 'a';

switch(bloodgroup){
case BLGR_A:
System.out.print("A");
case BLGR_B:
System.out.print("B");
case BLGR_O:
System.out.print("O");
break;
}
}
}

8 / 10

8. What will happen when you compile and run the following code?

public class Test{
public static void main(String[] args){

char c = 'd';
switch(c){
case 'a' :
System.out.print("a");
case 'b' :
System.out.print("b");
case 'c' :
System.out.print("c");
case 100 :
System.out.print("100");
default :
System.out.print("No match");
}
}

}

9 / 10

9. What will happen when you compile and run the following code?

public class Example {
public static void main(String[] args){
int time = 8;
switch(time){
case 7:
System.out.println("Let's drink");
case 9:
System.out.println("Good night");
case 010:
System.out.println("Let's eat");
}
}
}

10 / 10

10. What will happen when you compile and run the following code?

public class Example {

public static void main(String[] args){
int i = 10;
int j = 25;
System.out.println(i + ' ' + j);
}
}

Your score is

The average score is 12%

0%