Skip to content

Java Language Basics Quiz Online Test – 6

Java language basics quiz 6 contains 10 single and multiple choice questions. Java language basics quiz 6 questions are designed in such a way that it will help you understand the fundamental concepts of Java. At the end of the quiz, result will be displayed along with your score and Java language basics quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java language basics quiz online.

  1. Which of the following are correct syntax for declaring an array?

  2. 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());
    	}
    }
    
  3. 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());
    	}
    }
    
  4. What will happen when you compile and run the following code?

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

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test{
    	
    	public static void main(String[] args){	
    		List listColors = new ArrayList<>();
    		listColors.add("Red");
    		listColors.add("Green");
    		listColors.add("Blue");
    		
    		changeMe(listColors);
    		
    		System.out.println(listColors);
    	}
    
    	private static void changeMe(final List listColors) {
    		listColors.add("Cyan");
    		listColors.remove("Blue");
    	}		
    }
    
  6. main is a keyword in Java

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

    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");
    	}		
    }
    
  8. Will this code compile without any errors?

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

    public class Test{
    	
    	public static void main(String[] args){
    		int[] arr = {0, 1, 2};
    		
    		for(int i : arr)
    			System.out.print(i);
    		
    		arr.length = 2;
    
    		for(int i : arr)
    			System.out.print(i);
    	}		
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{
    	
    	public static void main(String[] args){
    		int[] a1 = {0, 1, 2};
    		int[] a2 = {0, 1, 2};
    		
    		if(a1 == a2)
    			System.out.print("==");
    		else
    			System.out.print("Not==");
    		
    		if(a1.equals(a2))
    			System.out.print("Equals");
    		else
    			System.out.print("NotEquals");
    	}		
    }