Skip to content

Java Assertion Quiz Online Test – Easy

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.

  1. 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";
    	}
    
    }
    
  2. Running a java program with -enableassertions switch will enable assertion for your program

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

  4. 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);
    	}
    	
    }
    
  5. Only way to enable assertion for your program is by using command line arguments.

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

  7. Program throws ___________ if assert statement fails.

  8. 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 + ",");
    		
    		
    	}
    	
    }
    
  9. 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);
    	}
    	
    }
    
  10. 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;
    	}
    	
    }