Skip to content

Java Declaration Initialization and Access Control Quiz 6

Java declaration, initialization and access control quiz 5 contains 10 single 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. Will this code compile?

    public class Test{
    	
    	String className = "Test";
    	
    	public static void main(String[] args) {
    		
    		final Test t = new Test();
    		t.className = "Test.class";
    	}
    }
    
  2. Which of the following are correct syntax for declaring an array?

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

    public class Test{
    	
    	public static void main(String[] args) {
    		Boolean b = new Boolean("TestClass");
    		System.out.println(b);
    	}
    }
    
  4. What will happen when you compile and run the following code?

    class Color{	
    	String name;	
    	public Color(String name){
    		this.name = name;
    	}	
    	public String getName(){
    		return name;
    	}	
    	public void setName(String name){
    		this.name = name;
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){			
    		Color c1 = new Color("Red");
    		changeColor(c1);
    		System.out.print(c1.getName());
    	}	
    	
    	public static void changeColor(Color c){
    		c.setName("Blue");
    		System.out.print(c.getName());
    	}
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){		
    		String[] strArray = new String[2];
    		System.out.println("Element: " + strArray[0]);
    	}
    }
    
  6. What will happen when you compile and run the following code?

    class Color{	
    	String name;	
    	public Color(String name){
    		this.name = name;
    	}	
    	public String getName(){
    		return name;
    	}	
    	public void setName(String name){
    		this.name = name;
    	}
    }
    
    public class Test{
    	
    	public static void main(String[] args){				
    		Color c1 = new Color("Red");
    		changeColor(c1);
    		System.out.print(c1.getName());
    	}	
    	
    	public static void changeColor(Color c){
    		c = new Color("Blue");
    		System.out.print(c.getName());
    	}
    }
    
  7. A final method cannot be overloaded.

  8. Will this code compile?

    public class Test{
    	
    	public static void main(String[] args){
    		
    		int array[][] = new int[2][];
    		array[0] = new int[5];
    		array[1] = new int[10];
    		
    	}
    }
    
  9. A final class can be abstract.

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

    public class Test{
    	
    	public static void main(String[] args) {
    		
    		float f1 = 10;
    		float f2 = 10.2;
    		
    		System.out.println(f1 + "" + f2);
    	}
    }