Skip to content

Java Declaration Initialization and Access Control Quiz 8

Java declaration, initialization and access control quiz 8 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) {
    		Float f = new Float(10.0);
    		Double d = new Double(10.0);
    		
    		if(f.equals(d))
    			System.out.println("Equal");
    		else
    			System.out.println("Not Equal");
    	}
    }
    
  2. What will happen when you compile and run the following code assuming file name is Test.java with below given commands?
    javac Test.java
    java Test

    public class Test{
    	public void sayHi(){
    		System.out.print("Hi");
    	}
    }
    
    class AnotherTest{
    	public static void main(String[] args) {
    		
    		System.out.print("Hello");
    		Test t = new Test();
    		t.sayHi();
    	}
    }
    
  3. Will this code compile without any errors?

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

    public class Test{
    	public static void main(String[] args) {
    		System.out.println("Hello");
    		new One().sayHi();
    	}
    }
    
    public class One{
    	public void sayHi(){
    		System.out.println("Hi");
    	}
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{
    	
    	public void inc(int number){
    		static int count = number;
    	}
    	
    	public static void main(String[] args) {
    		Test t1 = new Test();
    		t1.inc(1);
    		
    		Test t2 = new Test();
    		t2.inc(2);		
    	}
    }
    
  6. Can you declare a static method final?

  7. Will this code compile successfully?

    public class Test{
    	
    	public static void main(String[] args) {
    		int[] intArray[]  = new int[5][];
    	}
    }
    
  8. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public Test(int num){
    		num = num * 5;
    		System.out.print(num);
    	}
    	
    	public static void main(String[] args) {
    		int num = 1;
    		Test t = new Test(num);
    		System.out.print(num);
    	}
    }
    
  9. What will happen when you compile and run the following code?

    public class Test {
    	
    	public static void main(String[] args){		
    
    	float f = 10.0/2f;
    	System.out.println(f);
    	}
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{
    	static int i;
    	
    	public static void main(String[] args) {
    		Test t1 = new Test();
    		t1.i = 1;
    		
    		Test t2 = new Test();
    		t2.i = 2;
    		
    		System.out.println(t1.i + " " + t2.i);		
    	}
    }