JAVA QUIZ 15

26
Created on By Sirjana GhimireAcharya

JAVA QUIZ 15

1 / 10

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

import java.util.ArrayList;
import java.util.List;

public class Test{

public static void main(String[] args){
List listColors = new ArrayList<>();
listColors.add("Red");
listColors.add("Green");
listColors.add("Blue");

changeMe(listColors);

System.out.println(listColors);
}

private static void changeMe(final List listColors) {
listColors.add("Cyan");
listColors.remove("Blue");
}
}

2 / 10

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

public class Test {

public static void main(String[] args){

int temperature = 33;

if(temperature < 0)
System.out.println("Freezing");
else if(temperature < 30)
System.out.println("Pleasant");
else if(temperature < 50)
System.out.println("Hot");
else
System.out.println("Boiling");

}
}

3 / 10

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

public class Test {

public static void main(String[] args){

int i = 49;

if(i < 50){
System.out.println("Greater than 50");
System.out.println("Great");
}else
System.out.println("Less than 50");

}
}

4 / 10

4.Select all true statements from the following statements

5 / 10

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

public class Test{

public static void main(String[] args){
displayAge(20);
}

private static void displayAge(int age){
assert age >= 21 : getAgeMessage();
System.out.println(age);
}

private static String getAgeMessage() {
return "Your age must be greater than 21";
}

}

6 / 10

6. Running a java program with -enableassertions switch will enable assertion for your program

7 / 10

7. Running a java program with -ea switch will enable assertion in system classes.

8 / 10

8. Only way to enable assertion for your program is by using command line arguments.

9 / 10

9. If the Java program is using assertions, it must be run with -ea or -enableassertions switches.

10 / 10

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

public class Test{

public static void main(String[] args){

int[] marks = {40, 38, 52};
boolean[] pass = {false, false, false};

for(int i = 0 ; i < marks.length; i++){

try{

assert marks[i] >= 40 : pass[i] = true;

}catch(AssertionError ae){
pass[i] = false;
}
}

for(boolean b : pass)
System.out.print(b + ",");
}
}

Your score is

The average score is 16%

0%