Java Assertion Quiz contains 15 single and multiple choice questions of hard 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 15 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
11
12
13
14
15
Answered
Review
Question 1 of 15
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){
int age = 20;
assert age > 20 : getMessage();
System.out.println("Valid");
}
private static void getMessage() {
System.out.println("Not valid");
}
}
Correct answer.
Option 1 is the correct choice. The code will give compilation error.
You can use a method call in the assert statement after colon (:) provided it returns a value. The method with return type void cannot be used as second expression in the assert statement. The program will give compilation error saying “Expression must return a value”.
Incorrect answer.
Option 1 is the correct choice. The code will give compilation error.
You can use a method call in the assert statement after colon (:) provided it returns a value. The method with return type void cannot be used as second expression in the assert statement. The program will give compilation error saying “Expression must return a value”.
Question 2 of 15
2. 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 false;
System.out.println("True");
}
}
Correct answer.
Option 2 is the correct choice. Compiler will not complain about unreachable code and program will compile without any compilation error. However, it will throw AssertionError due to assert false; statement.
Incorrect answer.
Option 2 is the correct choice. Compiler will not complain about unreachable code and program will compile without any compilation error. However, it will throw AssertionError due to assert false; statement.
Question 3 of 15
3. Question
Consider below given code. What will you write at the line number 13 to make sure that every car object has minimum 3 and maximum 4 wheels using assertion?
public class Test{
public static void main(String[] args){
Car fordCar = new Car(4, "Model1");
displayCar(fordCar);
Car triCar = new Car(3, "Model2");
displayCar(triCar);
}
private static void displayCar(Car car) {
//your code here
System.out.println(car.model + ", " + car.wheels);
}
}
class Car{
int wheels;
String model;
Car(int wheels, String model){
this.wheels = wheels;
this.model = model;
}
}
Correct answer.
Option 3 is the correct choice. Assert statement must return true in order for the program to continue execution. If the expression is false, AssertionError is thrown.
As per requirement, number of wheels must be between 3 and 4, both inclusive. Option 1 does exactly opposite of that. Option 2 does not check for the minimum number of wheels. Option 4 uses OR condition, so if any one of them is true, whole condition will be true. Option 3 is valid check because it checks if the number wheels are greater than or equal to 3 and less than or equal to 4.
Incorrect answer.
Option 3 is the correct choice. Assert statement must return true in order for the program to continue execution. If the expression is false, AssertionError is thrown.
As per requirement, number of wheels must be between 3 and 4, both inclusive. Option 1 does exactly opposite of that. Option 2 does not check for the minimum number of wheels. Option 4 uses OR condition, so if any one of them is true, whole condition will be true. Option 3 is valid check because it checks if the number wheels are greater than or equal to 3 and less than or equal to 4.
Question 4 of 15
4. Question
Consider below given code. Will it compile without any errors?
public class Test{
public static void main(String[] args){
boolean b = false;
assert b = true;
}
}
Correct answer.
Option 1 is the correct choice. Assert statement needs boolean expression and b = true is valid boolean expression. So code will compile without any errors.
Incorrect answer.
Option 1 is the correct choice. Assert statement needs boolean expression and b = true is valid boolean expression. So code will compile without any errors.
Question 5 of 15
5. Question
What will happen when you compile and run the following code using below given command?
java -ea Test
public class Test{
public static void main(String[] args){
assert checkArguments(args) : "Arguments empty";
System.out.println("Arguments are fine");
}
private static boolean checkArguments(String[] args) {
if(args[0] != "")
return true;
return false;
}
}
Correct answer.
Option 4 is the correct choice. There is nothing wrong with the program, and it will compile fine. Syntax of assert statement is also correct.
However, since we haven’t passed any command line arguments, args array will be empty. Accessing first element using args[0] inside checkArguments method will throw java.lang.ArrayIndexOutOfBoundsException exception.
Incorrect answer.
Option 4 is the correct choice. There is nothing wrong with the program, and it will compile fine. Syntax of assert statement is also correct.
However, since we haven’t passed any command line arguments, args array will be empty. Accessing first element using args[0] inside checkArguments method will throw java.lang.ArrayIndexOutOfBoundsException exception.
Question 6 of 15
6. Question
What will happen when you compile and run the following code with assertion enabled?
public class Test{
public static void main(String[] args){
divide(10, 3);
divide(17, 4);
}
public static void divide(int a, int b){
assert (a == 0 || b == 0) : return;
int c = a/b;
System.out.print(c + " ");
}
}
Correct answer.
Option 1 is the correct choice. You cannot use return statement as second expression of assert statement in Java. Code will give compilation error at assert statement.
Incorrect answer.
Option 1 is the correct choice. You cannot use return statement as second expression of assert statement in Java. Code will give compilation error at assert statement.
Question 7 of 15
7. Question
What will happen when you compile and run the following code with following command?
java Test
public class Test{
public static void main(String[] args){
int i = 10;
int j = 24;
int k = 34;
assert i + j > k : k--;
System.out.println(i + j + k);
}
}
Correct answer.
Option 2 is the correct choice. The program was not invoked using -ea or -enableassertions command line switch to enable the assertion. Hence, all the assert statements are ignored. So variable k is not decremented and its value remains same. The code will output 68 when run.
Incorrect answer.
Option 2 is the correct choice. The program was not invoked using -ea or -enableassertions command line switch to enable the assertion. Hence, all the assert statements are ignored. So variable k is not decremented and its value remains same. The code will output 68 when run.
Question 8 of 15
8. Question
It is possible to enable assertions only for classes within specific package.
Correct answer.
Option 1 is the correct choice. Assertion can be enabled for specific packages.
You can specify the package name using “java -ea: class_name” command. For example, for enabling assertions for all the classes in package com.javacodeexamples.tools, you need to use “java -ea:com.javacodeexamples.tools Test” command.
Similarly, to disable the assertion for classes within specific packages/sub-packages, you can use -da switch instead of -ea switch. For example, “java -da:com.javacodeexamples.tools Test” will enable assertions for all the classes except for the classes within com.javacodeexamples.tools packages.
Incorrect answer.
Option 1 is the correct choice. Assertion can be enabled for specific packages.
You can specify the package name using “java -ea: class_name” command. For example, for enabling assertions for all the classes in package com.javacodeexamples.tools, you need to use “java -ea:com.javacodeexamples.tools Test” command.
Similarly, to disable the assertion for classes within specific packages/sub-packages, you can use -da switch instead of -ea switch. For example, “java -da:com.javacodeexamples.tools Test” will enable assertions for all the classes except for the classes within com.javacodeexamples.tools packages.
Question 9 of 15
9. Question
It is possible to enable assertions for specific classes and disable assertion for specific classes (both at the same time) while running the program using command line arguments?
Correct answer.
Option 1 is the correct choice. You can enable and disable assertions for specific classes using -ea and -da switches respectively in the same command.
For example, “java -ea:com.javacodeexamples.tools -da:com.javacodeexamples.tools.gui Test” command will enable assertions for classes inside com.javacodeexamples.tools package but disable assertion for com.javacodeexamples.tools.gui sub-package.
Incorrect answer.
Option 1 is the correct choice. You can enable and disable assertions for specific classes using -ea and -da switches respectively in the same command.
For example, “java -ea:com.javacodeexamples.tools -da:com.javacodeexamples.tools.gui Test” command will enable assertions for classes inside com.javacodeexamples.tools package but disable assertion for com.javacodeexamples.tools.gui sub-package.
Question 10 of 15
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){
getCircleArea(0);
}
public static double getCircleArea(int radius){
double area = 0;
try{
System.out.print("Calculating area: ");
assert radius > 0;
area = 3.14 * radius * radius;
System.out.println(area);
}catch(Error e){
System.out.println("Exception");
}
return area;
}
}
Correct answer.
Option 3 is the correct choice. The program will not give any compilation error. Since the radius is not greater than 0, assert expression will be evaluated to be false and AssertionError will be thrown. Error is the super class of AssertionError class, so control will go to catch block and “Exception” will be printed.
Incorrect answer.
Option 3 is the correct choice. The program will not give any compilation error. Since the radius is not greater than 0, assert expression will be evaluated to be false and AssertionError will be thrown. Error is the super class of AssertionError class, so control will go to catch block and “Exception” will be printed.
Question 11 of 15
11. Question
What is wrong with the following code?
public class Test{
public static void main(String[] args){
assert args.length > 0 : "I expect one argument";
System.out.println(args[0]);
}
}
Correct answer.
Option 3 is the correct choice. At the first look it appears that there is nothing wrong with the code. The code checks whether there is at least one argument passed using assertion and if the assertion is successful, it prints the first argument.
However, what happens when assertion is not enabled while running the program? It will bypass the argument length assertion check and it will try to print the first argument which may have not been passed. For the same reason, Java discourages using assertions for public method argument validations.
Incorrect answer.
Option 3 is the correct choice. At the first look it appears that there is nothing wrong with the code. The code checks whether there is at least one argument passed using assertion and if the assertion is successful, it prints the first argument.
However, what happens when assertion is not enabled while running the program? It will bypass the argument length assertion check and it will try to print the first argument which may have not been passed. For the same reason, Java discourages using assertions for public method argument validations.
Option 3 is the correct choice. Assertion is not used properly in the removeName method.
The removeName method removes an element in an assert statement and checks for result by using the return value of ArrayList’s remove method. If the remove operation is successful, method returns true, otherwise it will throw AssertionError along with the “Not Removed” message.
What happens when assertion is not enabled when running the program? The method which is supposed to remove a name from ArrayList will not remove the name because all assert statements are skipped if the assertion is not enabled during run time.
Including an action (like adding or removing elements) in assert statement is not recommended due to the same reason. Actions should be performed in a separate statements and only results should be checked in the assert statement.
Incorrect answer.
Option 3 is the correct choice. Assertion is not used properly in the removeName method.
The removeName method removes an element in an assert statement and checks for result by using the return value of ArrayList’s remove method. If the remove operation is successful, method returns true, otherwise it will throw AssertionError along with the “Not Removed” message.
What happens when assertion is not enabled when running the program? The method which is supposed to remove a name from ArrayList will not remove the name because all assert statements are skipped if the assertion is not enabled during run time.
Including an action (like adding or removing elements) in assert statement is not recommended due to the same reason. Actions should be performed in a separate statements and only results should be checked in the assert statement.
Question 13 of 15
13. 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 counter = 0;
try{
counter += 10;
assert counter - 10 < 10 : counter +=10;
}catch(Error e){
counter -= 10;
}
System.out.println(counter);
}
}
Correct answer.
Option 2 is the correct choice. There is no compilation error in the program. Variable counter is initialized with 0 and 10 is added to it in the try block. Assert statement checks if the counter – 10 is less than 10, which is true (because 10 – 10 < 10). Since the first expression of the assert statement is true, the second expression after : is not evaluated at all. Plus, program will not throw AssertionError as well. So value of the counter variable remains 10. Error is the parent class of AssertionError, so it is valid to catch Error instead of AssertionError.
Incorrect answer.
Option 2 is the correct choice. There is no compilation error in the program. Variable counter is initialized with 0 and 10 is added to it in the try block. Assert statement checks if the counter – 10 is less than 10, which is true (because 10 – 10 < 10). Since the first expression of the assert statement is true, the second expression after : is not evaluated at all. Plus, program will not throw AssertionError as well. So value of the counter variable remains 10. Error is the parent class of AssertionError, so it is valid to catch Error instead of AssertionError.
Question 14 of 15
14. Question
There is a method to compute rectangle area which accepts two arguments width and height respectively. These two arguments must be greater than zero. Do you think the below given code implements the method properly?
public static int getArea(int width, int height){
assert (width > 0 && height > 0) : "Invalid Parameters";
return width * height;
}
Correct answer.
No is the correct choice. Java specification discourages using assertions for parameter validations in a public methods. Reason is, assertion can be enabled or disabled at run time. If the method is invoked with assertion disabled, width and height validation is never executed and it compromises the validity of the arguments.
Incorrect answer.
No is the correct choice. Java specification discourages using assertions for parameter validations in a public methods. Reason is, assertion can be enabled or disabled at run time. If the method is invoked with assertion disabled, width and height validation is never executed and it compromises the validity of the arguments.
Question 15 of 15
15. Question
There is a method to compute rectangle area which accepts two arguments width and height respectively. The computed area must be greater than zero. Do you think the below given code implements the method properly?
public static int getArea(int width, int height){
int area = width * height;
assert area > 0 : "Invalid area";
return area;
}
Correct answer.
Yes is the correct choice. As per Java specifications, assertion can be used to validate post conditions in public as well as private methods. However, parameter validation using assertion for public method is discouraged.
Incorrect answer.
Yes is the correct choice. As per Java specifications, assertion can be used to validate post conditions in public as well as private methods. However, parameter validation using assertion for public method is discouraged.