Skip to content

Java Assertion Quiz Online Test – Difficult

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.

  1. 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");
    	}
    	
    }
    
  2. 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");
    		
    	}	
    }
    
  3. 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;
    	}	
    }
    
  4. 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;
    	}
    }
    
  5. 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;
    	}
    	
    }
    
  6. 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 + " ");
    	}	
    }
    
  7. 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);		
    	}
    	
    }
    
  8. It is possible to enable assertions only for classes within specific package.

  9. 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?

  10. 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;
    	}
    }
    
  11. 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]);
    	}
    	
    }
    
  12. What is wrong with the below given code?

    import java.util.ArrayList;
    
    public class Test{
    	
    	public static void main(String[] args){
    		
    		 ArrayList listNames = new ArrayList();
    		 addNames(listNames);
    		
    		 System.out.println( removeJohn(listNames) );
    	}
    
    	private static boolean removeJohn(ArrayList listNames) {
    		assert listNames.remove("John") : "Not Removed";
    		return true;
    	}
    
    	private static void addNames(ArrayList listNames) {
    		listNames.add("John");
    		listNames.add("Mike");
    		listNames.add("Charlie");		
    	}
    	
    }
    
  13. 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);
    	}
    	
    }
    
  14. 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;
    }
    
  15. 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;
    }