Java Declaration Initialization and Access Control Quiz 4

Java declaration, initialization and access control quiz 4 contains 10 single and multiple choice questions. Declaration, initialization and access control quiz questions are designed in such a way that it will help you understand how to declare and initialize variables in Java as well as how to properly define access control for class, member variables and methods. At the end of the quiz, 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 Declaration, Initialization and Access Control quiz online.

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

    public class Test{
    	
    	public static void main(String[] args) {
    		int i = 10 + 011 + 12;
    		System.out.println(i);
    	}
    }
    
    
  2. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args) {
    		int i = 10+0x12;
    		System.out.println(i);
    	}
    }
    
  3. A constructor cannot throw any exception. It must handle all the exception itself using try catch block.

  4. A constructor can be synchronized.

  5. An interface cannot define an inner class.

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

    public class Test{
    	
    	static String time = getTime();
    	static String currentTime = "5:30";
    	
    	private static String getTime() {
    		return currentTime;
    	}
    	
    	public static void main(String[] args) {
    		System.out.println(time);
    	}
    }
    
  7. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    		int[] intarr = new int[2]{4, 5, 6};
    		for(int i = 0; i < intarr.length; i++)
    			System.out.print(intarr[i]);
    	}
    }
    
  8. Will this code compile?

    import java.util.ArrayList;
    
    public class Test{
    	
    	public static void main(String[] args) {
    		final ArrayList aList =
    			new ArrayList();
    		
    		aList.add("Hello");
    	}
    }
    
  9. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args) {
    		int i = 1, j = 2;
    		swap(i, j);
    		System.out.println(i + " " + j);
    	}
    
    	private static void swap(int i, int j) {
    		int x = i;
    		i = j;
    		j = x;		
    	}
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){		
    		double d = 10.0/0;
    		System.out.println(d);
    	}
    }
    

Online Shopping