Java Assertion Quiz contains 10 single and multiple choice questions of easy difficulty level. Assertion quiz test questions are designed in such a way that it will help you understand how assertion works in Java. At the end of the quiz mock exam, result will be displayed along with your score and quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java Assertion quiz online.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 10 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
Assertions0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
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";
}
}
Correct answer.
Option 2 is the correct choice. Since the first expression in the assertion age >= 21 is false, second expression will be evaluated that is a method call in our case. getAgeMessage method is called which returns a string “Your age must be greater than 21”. This message will be included along with the AssertionError since the assertion failed.
Remember, assertion condition must be true (first expression after assert keyword) to continue with the program. If it is false, second expression will be evaluated and its output will be included along with the AssertionError message.
Incorrect answer.
Option 2 is the correct choice. Since the first expression in the assertion age >= 21 is false, second expression will be evaluated that is a method call in our case. getAgeMessage method is called which returns a string “Your age must be greater than 21”. This message will be included along with the AssertionError since the assertion failed.
Remember, assertion condition must be true (first expression after assert keyword) to continue with the program. If it is false, second expression will be evaluated and its output will be included along with the AssertionError message.
Question 2 of 10
2. Question
Running a java program with -enableassertions switch will enable assertion for your program
Correct answer.
True is the correct choice. Assertion for your Java program can be enabled either by using -ea switch or -enableassertions switch.
Incorrect answer.
True is the correct choice. Assertion for your Java program can be enabled either by using -ea switch or -enableassertions switch.
Question 3 of 10
3. Question
Running a java program with -ea switch will enable assertion in system classes.
Correct answer.
False is the correct choice. To enable assertion in system classes you must you -esa or -enablesystemassertions switch.
Incorrect answer.
False is the correct choice. To enable assertion in system classes you must you -esa or -enablesystemassertions switch.
Question 4 of 10
4. Question
What will happen when you compile and run the following code with assertion enabled?
public class Test{
private static int ALLOW_OBJECTS = 1;
Test(){
assert ALLOW_OBJECTS;
ALLOW_OBJECTS = 0;
}
public static void main(String[] args){
Test t = new Test();
t.getState();
}
private void getState() {
System.out.println(ALLOW_OBJECTS);
}
}
Correct answer.
Option 1 is the correct choice. The first expression after assert keyword must be of type boolean. The program will give compilation error “Type mismatch: cannot convert from int to boolean”.
Incorrect answer.
Option 1 is the correct choice. The first expression after assert keyword must be of type boolean. The program will give compilation error “Type mismatch: cannot convert from int to boolean”.
Question 5 of 10
5. Question
Only way to enable assertion for your program is by using command line arguments.
Correct answer.
False is the correct choice. Assertions can be enabled or disabled programmatically as well. ClassLoader class provides two methods to do that.
1. void setClassAssertionStatus(String className, boolean enabled)
This method is used to enable or disable assertion for specific class.
2. void setPackageAssertionStatus(String packageName, boolean enabled)
This method is used to enable or disable assertion for packages.
Incorrect answer.
False is the correct choice. Assertions can be enabled or disabled programmatically as well. ClassLoader class provides two methods to do that.
1. void setClassAssertionStatus(String className, boolean enabled)
This method is used to enable or disable assertion for specific class.
2. void setPackageAssertionStatus(String packageName, boolean enabled)
This method is used to enable or disable assertion for packages.
Question 6 of 10
6. Question
If the Java program is using assertions, it must be run with -ea or -enableassertions switches.
Correct answer.
False is the correct choice. You can run the program which uses assertions without assertion command line arguments. Assertion statements will not run in that case.
Incorrect answer.
False is the correct choice. You can run the program which uses assertions without assertion command line arguments. Assertion statements will not run in that case.
Question 7 of 10
7. Question
Program throws ___________ if assert statement fails.
Correct answer.
Option 2 is the correct choice. Java program throws java.lang.AssertionError if the assert statement fails (i.e. boolean condition of the assert statement is evaluated to be false).
Incorrect answer.
Option 2 is the correct choice. Java program throws java.lang.AssertionError if the assert statement fails (i.e. boolean condition of the assert statement is evaluated to be false).
Question 8 of 10
8. Question
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 + ",");
}
}
Correct answer.
Option 2 is the correct choice. Code will compile without any errors. Second expression of assert statement is not evaluated if the first expression of assert statement is true. It only gets evaluated if and only if the first expression is false.
In the above code, pass[i] = 40 is not evaluated in case the mark element is greater than or equal to 40. It is evaluated only if the mark element is less than 40, but in that case AssertionError is also thrown after evaluation which again sets the pass element to false. So output will be “false,false,false,”.
Incorrect answer.
Option 2 is the correct choice. Code will compile without any errors. Second expression of assert statement is not evaluated if the first expression of assert statement is true. It only gets evaluated if and only if the first expression is false.
In the above code, pass[i] = 40 is not evaluated in case the mark element is greater than or equal to 40. It is evaluated only if the mark element is less than 40, but in that case AssertionError is also thrown after evaluation which again sets the pass element to false. So output will be “false,false,false,”.
Question 9 of 10
9. Question
What will happen when you compile and run the following code with command “java Test -10”?
public class Test{
public static void main(String[] args){
int temperature = Integer.parseInt(args[0]);
assert temperature > 0 : temperature = 0;
System.out.println(temperature);
}
}
Correct answer.
Option 4 is the correct choice. There is no compilation error in the code. Since we have not passed -ea or -enableassertions switches while running the program, assert statement is not executed at all. Temperature variable remains same and program will output -10 when run.
It is not a good practice to change your variable values in assert statement and rely on it. Reason is, if the program is run with assertion disabled, program will not behave as per the expectation.
Incorrect answer.
Option 4 is the correct choice. There is no compilation error in the code. Since we have not passed -ea or -enableassertions switches while running the program, assert statement is not executed at all. Temperature variable remains same and program will output -10 when run.
It is not a good practice to change your variable values in assert statement and rely on it. Reason is, if the program is run with assertion disabled, program will not behave as per the expectation.
Question 10 of 10
10. Question
What will happen when you compile and run the following code with assertion enabled?
public class Test{
public static void main(String[] args){
assert getAge() > 20 : "Not valid";
System.out.println("Valid");
}
private static int getAge() {
return 21;
}
}
Correct answer.
Option 4 is the correct choice. You can use method call in the assert statement, provided that the first expression after the assert keyword must be of type boolean. Since the getAge method returns 21 and it is greater than 20, AssertionError will not be thrown and the next System.out.println will print “Valid”.
Incorrect answer.
Option 4 is the correct choice. You can use method call in the assert statement, provided that the first expression after the assert keyword must be of type boolean. Since the getAge method returns 21 and it is greater than 20, AssertionError will not be thrown and the next System.out.println will print “Valid”.