Ticker

6/recent/ticker-posts

Ad Code

Java Programming Practice MCQs Codetantra. CBT-6

Java Programming Practice MCQs Codetantra. CBT-6

The topics to be included in that will be Files and IO Streams, Annotations & Assertions (L53-55), and Collections (L56-60) And PEV106 Practice MCQs


01 What will be the output of the following code snippet?


 	Map <Integer, String> map= new HashMap<>();
	map.put(5, "Amit");
	map.put(3, "Naveen");	
	map.put(2, "Amit");	
	map.put(5, "Rajan");	
	map.put(null, "Sanchit");
	System.out.println(map.get(null) + " " + map.get(5)); 
  •  

    Sanchit Amit

  •  

    Sanchit Rajan

  •  

    NullPointerException

  • Compilation Error: Incompatible types


02. Suppose e:\Unit4 and d:\Java\ABC.txt exists but

e:\Unit4\CBT6.txt does not exist.


What will happen when the following program is compiled and executed?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	 
	Path p2 = Paths.get(e:\\Unit4\\CBT6.txt);     
	Files.copy(p1, p2);
  }
}
  •  

     CBT6.txt will be created inside e:\Unit4 directory but

    d:\Java\ABC.txt will not be removed

  •  

    CBT6.txt will be created inside e:\Unit4 directory and 

    d:\Java\ABC.txt will be removed

  •  

    Code will not compile

  •  

    NoSuchFileException will occur


03. Suppose e:\Unit4 and d:\Java\ABC.txt exists but

e:\Unit4\CBT6.txt does not exist.


What will happen when the following program is compiled and executed?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	 
	Path p2 = Paths.get(e:\\Unit4\\CBT6.txt);     
	Files.move(p1, p2);
  }
}
  •  

    CBT6.txt will be created inside e:\Unit4 directory and 

    d:\Java\ABC.txt will be removed

  •  

    CBT6.txt will be created inside e:\Unit4 directory but

    d:\Java\ABC.txt will not be removed

  •  

    Code will not compile

  •  

    NoSuchFileException will occur


04. What will be the output of the following code snippet?


 	HashMap <Integer, String> map= new TreeMap<>();
	map.put(5, "Amit ");
	map.put(3, "Naveen ");	
	map.put(2, "Amit ");	
	map.put(3, "Rajan ");	
	map.put(5, "Sanchit "); 
	Collection <String> val = map.values();
	System.out.println(val); 
  •  

    [Amit, Naveen, Rajan, Sanchit]

  •  

    [Amit, Naveen, Sanchit]

  •  

    Compilation Error

  •  

    [Amit, Rajan, Sanchit]


05. What will be the output of the following code snippet?


	Comparator <Integer> c = (x, y) -> {
			if(x > y) return 1;
			else return -1;
			     };
 	Set <Integer> set= new TreeSet<>(c);
	set.add(5);
	set.add(3);
	set.add(4);
	set.add(2);
	set.add(3);
	System.out.println(set);  
  •  

    [5, 4, 3, 3, 2]

  •  

    [5, 4, 3, 2]

  •  

    [2, 3, 4, 5]

  •  

    [2, 3, 3, 4, 5]


06. Which of the following statement is TRUE about the following program?


import java.io.*;

class Demo
{
  public static void main(String [] arg) 
  {
     try(FileInputStream fin = new FileInputStream("d:\\Java\\abc.txt");
	FileOutputStream fout = new FileOutputStream("result.txt"))
       {
	byte b = fin.read();  //Line 1
	fout.write(b);      //Line 2
       }	
     catch(IOException ex) { System.out.println(ex); }
  }
}
  •  

    Compilation Error in Line 1

  •  

    Compilation Error in Line 2

  •  

    abc.txt and result.txt files must exist to execute the program successfully.

  •  

    It will throw FileNotFoundException is result.txt is not present.


07. Which of the following statement is TRUE about the following program?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	     
	Files.delete(p1);
  }
} 
  •  

    NoSuchFileException will be thrown if ABC.txt file does not exist.

  •  

    FileNotFoundException will be thrown if ABC.txt file does not exist.

  •  

    DirectoryNotEmptyException will be thrown if the directory d:\Java is not empty.

  •  

    None of These


08. What will be the output of the following code snippet?


	ArrayList <Integer> al = new ArrayList<>();
	al.add(5);   al.add(2);
 	Set <Integer> set= new TreeSet<>((a, b) -> a > b ? 1 : -1 );
	set.add(5);
	set.add(3);
	set.add(5);
	set.remove(2);
	set.addAll(al);
	System.out.println(set);
  •  

    [2, 3, 5]

  •  

    [5, 3, 2]

  •  

    None of These

  •  

    [3, 5]


09. What will be the output of the following code snippet?


 	Map <Integer, String> map= new TreeMap<>();
	map.put(5, "Amit ");
	map.put(3, "Naveen ");	
	map.put(2, "Amit ");	
	map.put(3, "Rajan ");	
	map.put(5, "Sanchit "); 
	Collection <String> val = map.values();
	System.out.println(val); 


  •  

    [Amit, Naveen, Rajan, Sanchit]

  •  

    [Amit, Naveen, Sanchit]

  •  

    Compilation Error

  •  

    [Amit, Rajan, Sanchit]


10. What will be the output of the following code snippet?


	ArrayList <Integer> al = new ArrayList<>();
	al.add(5);  al.add(2);
 	Set <Integer> set= new LinkedHashSet<>();
	set.add(5);
	set.add(3);
	set.add(5);
	set.remove(2);
	set.addAll(al);
	System.out.println(set);
  •  

    [2, 3, 5]

  •  

    [5, 3, 2]

  •  

    None of These

  •  

    [3, 5]


11. Predict the output of below given code snippet:

import java.io.*;
import java.util.*;
class Main {	
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
			arrayList.add(48);
		    arrayList.add(80);
			arrayList.add(112);
	    	arrayList.remove(1);
		    System.out.println(arrayList);
		    System.out.print(arrayList.get(2));
	}
}



  •  

    [48, 112]

    Exception in thread "main" java.lang.IndexOutOfBoundsException

  •  

    [48, 112]

    80

  •  

    [80, 112]

    80

  •  

    error: cannot find symbol System.out.print(arrayList.get(1));

  •  

    [80, 112]

    112


12. Which of the following statement is TRUE about the following program?


import java.io.*;

class Demo
{
  public static void main(String [] arg) 
  {
     byte b = 65;
     try(FileOutputStream fout = new FileOutputStream("d:\\Java\\abc.txt", true))
       {
	fout.write(b);
       }	
     catch(IOException ex) { System.out.println(ex); }
  }
}


  •  

    It will append "A" in the file abc.txt if it is already existing.

  •  

    It will overwrite the content of abc.txt with "A" if abc.txt is already existing.

  •  

    Compilation Error

  •  

    It will throw FileNotFoundException is abc.txt is not present.


13. Suppose e:\Unit4 and d:\Java\ABC.txt exists but

e:\Unit4\CBT6.txt does not exist.


What will happen when the following program is compiled and executed?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	 
	Path p2 = Paths.get(e:\\Unit4\\CBT6.txt);     
	Files.copy(p2, p1);
  }
}
  •  

    NoSuchFileException will occur

  •  

    CBT6.txt will be created inside e:\Unit4 directory and 

    d:\Java\ABC.txt will be removed

  •  

    Code will not compile

  •  

    CBT6.txt will be created inside e:\Unit4 directory but

    d:\Java\ABC.txt will not be removed


14. Which of the following statement is TRUE about the following program?

import java.io.*;

class Demo
{
  public static void main(String [] arg) 
  {
     byte b = 65;
     try
       {
	FileOutputStream fout = new FileOutputStream("d:\\Java\\abc.txt", true);
	fout.write(b);
       }	
     catch(FileNotFoundException ex) { System.out.println(ex); }
  }
}
  •  

    Compilation Error: Unreported Exception

  •  

    It will always create a new file abc.txt and "A" will be written on that file.

  •  

    It will append "A" in the file abc.txt if it is already existing.

  •  

    It will throw FileNotFoundException is abc.txt is not present.


15. Which of the following statement is TRUE about the following program?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC");	     
	Files.delete(p1);
  }
} 


  •  

    DirectoryNotEmptyException will be thrown if the directory d:\Java\ABC is not empty.

  •  

    NoSuchDirectoryException will be thrown if ABC directory does not exist.

  •  

    None of These

  •  

    FileNotFoundException will be thrown if ABC does not exist.


16. What will be the output of the following code snippet?


	List <Integer> al = new ArrayList<>();
	al.add(5);
	al.add(7);
	al.add(4);
	al.add(1, 4);
	al.set(4, 9);
	al.remove(2);
	System.out.println(al);
  •  

    [4, 7, 9]

  •  

    [5, 7, 9]

  •  

    IndexOutOfBoundsException

  •  

    [5, 4, 9]


17. Which of the following statement is TRUE about the following program?


import java.io.*;

class Student implements Serializable { }
class PartTimeStudent extends Student { }

class Demo
{
  public static void main(String [] arg) 
  {
     try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("students.txt")))
       {
	out.writeObject(new PartTimeStudent());  //Line 1
	out.writeObject(new Student());         //Line 2
       }	
     catch(IOException ex) { System.out.println(ex); }
  }
}
  •  

    Objects of PartTimeStudent and Student both will be written in the file students.txt.

  •  

    It will throw NotSerializableException because PartTimeStudent is not Serializable.

  •  

    Compilation Error in Line 2

  •  

    Compilation Error in Line 1


18. What will be the output of the following code snippet?


 	Map <Integer, String> map= new LinkedHashMap<>();
	map.put(5, "Amit ");
	map.put(3, "Naveen ");	
	map.put(2, "Amit ");	
	map.put(3, "Rajan ");	
	map.put(5, "Sanchit "); 
	Set<Integer> keys = map.keySet();
	for(Integer k: keys)
	System.out.print(map.get(k) + " "); 
  •  

    Amit Rajan Sanchit

  •  

    Sanchit Rajan Amit

  •  

    Compilation Error

  •  

    Amit Rajan Sanchit


19.What will be the output of the following code snippet?


 	Map <Integer, String> map= new HashMap<>();
	map.put(5, "Amit ");
	map.put(3, "Naveen ");	
	System.out.print(map.put(2, "Amit "));	
	System.out.print(map.put(3, "Rajan "));	
	System.out.print(map.put(5, "Sanchit ")); 
  •  

    nullNaveen Amit

  •  

    Naveen Amit

  •  

    None of These

  •  

    Amit Rajan Sanchit


20.What will be the output of the following code snippet?


	Comparator <Integer> c = (x, y) -> {
			if(x < y) return 1;
			else return -1;
			   };
 	Set <Integer> set= new TreeSet<>(c);
	set.add(5);
	set.add(3);
	set.add(4);
	set.add(2);
	set.add(3);
	System.out.println(set);  


  •  

    [5, 4, 3, 3, 2]

  •  

    [5, 4, 3, 2]

  •  

    [2, 3, 4, 5]

  •  

    [2, 3, 3, 4, 5]


21.What will be the output of the following code snippet?


	ArrayList <Integer> al = new ArrayList<>();
	al.add(5);
	al.add(7);
	al.add(4);
	al.add(1, 4);
	al.set(3, 9);
	al.remove(2);
	System.out.println(al);


  •  

    [4, 7, 9]

  •  

    [5, 7, 9]

  •  

    IndexOutOfBoundsException

  •  

    [5, 4, 9]


22.What will be the output of the following code snippet?


	List <Integer> al = new ArrayList<>();
	al.add(5);
	al.add(7);
	al.add(4);
	al.add(1, 4);
	al.set(2, 3);
	al.remove(3);
	System.out.println(al);
  •  

    [4, 7, 3]

  •  

    [5, 7, 4]

  •  

    IndexOutOfBoundsException

  •  

    [5, 4, 3]


23. Predict the output of below given code snippet:

import java.util.*;
public class Main {
	public static void main(String args[]) {
		HashMap<Integer, String> hash = new HashMap<Integer, String>();
		hash.put(-8, "1");
		hash.put(-8, "9");
	    hash.put(-8, "14");
		System.out.println(hash.get(-8));
		for (Map.Entry<Integer, String> iterator : hash.entrySet())
			System.out.println(iterator.getKey() + ":" + iterator.getValue());
	}
}
  •  

    14

    -8:14

  •  

    1

    9

    14

    -8:14

  •  

    Compile time error

  •  

    14

    -8:1 9 14


24. Which of the following statement is TRUE about the following program?


import java.io.*;

class Demo
{
  public static void main(String [] arg) 
  {
     byte b = 65;
     try(FileOutputStream fout = new FileOutputStream("d:\\Java\\abc.txt", false))
       {
	fout.write(b);
       }	
     catch(IOException ex) { System.out.println(ex); }
  }
}


  •  

    It will overwrite the content of abc.txt with "A" if abc.txt is already existing.

  •  

    It will throw FileNotFoundException is abc.txt is not present.

  •  

    Compilation Error

  •  

    It will append "A" in the file abc.txt if it is already existing.


25. Predict the output of below given code snippet:

import java.io.*;
import java.util.*;
class Main {	
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
			arrayList.add(43);
		    arrayList.add(75);
			arrayList.add(107);
	    	arrayList.remove(1);
		    System.out.println(arrayList);
		    System.out.print(arrayList.get(1));
	}
}



  •  

    [43, 107]

    107

  •  

    [75, 107]

    107

  •  

    error: cannot find symbol System.out.print(arrayList.get(1));


  •  

    [75, 107]

    75


26.What will be the output of the following code snippet?


	List <Integer> al = new ArrayList<>();
	al.add(5);
	al.add(7);
	al.add(2);
	al.add(1, 4);
	al.add(2, 5);
	al.remove(2);
	System.out.println(al);
  •  

    [5, 4, 7, 2]

  •  

    [5, 4, 5, 7]

  •  

    IndexOutOfBoundsException

  •  

    [5, 5, 7, 2]


27. What will be the output of the following code snippet?


	ArrayList <Integer> al = new ArrayList<>();
	al.add(5);   
    al.add(2);
 	Set <Integer> set= new TreeSet<>();
	set.add(5);
	set.add(3);
	set.add(5);
	set.remove(2);
	set.addAll(al);
	System.out.println(set);
  •  

    [2, 3, 5]

  •  

    [5, 3, 2]

  •  

    None of These

  •  

    [3, 5]


28. What will be the output of the following code snippet?


	Set <Integer> set= new LinkedHashSet<>();
	set.add(5);
	set.add(3);
	set.add(5);
	set.add(2);
	set.remove(5);
	set.add(3);
	set.add(5);
	System.out.println(set);
  •  

    [2, 3, 5]

  •  

    [5, 3, 2]

  •  

    [3, 2, 5]

  •  

    [5, 2, 3]

29. What will happen when the following program is compiled and executed?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) 
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	 
	Path p2 = Paths.get(e:\\Unit4\\CBT6.txt);     
	Files.move(p1, p2);
  }
}


  •  

    Code will not compile

  •  

    NoSuchFileException will occur

  •  

    CBT6.txt will be created inside e:\Unit4 directory and 

    d:\Java\ABC.txt will be removed

  •  

     CBT6.txt will be created inside e:\Unit4 directory but

    d:\Java\ABC.txt will not be removed

30. Suppose d:\Java\ABC.txt exists but

e:\Unit4 does not exist.


What will happen when the following program is compiled and executed?


import java.nio.file.*;
import java.io.*;

class Demo
{
  public static void main(String [] arg) throws Exception
  {
	Path p1 = Paths.get("d:\\Java\\ABC.txt");	 
	Path p2 = Paths.get(e:\\Unit4\\CBT6.txt);     
	Files.move(p1, p2);
  }
}
  •  

    NoSuchFileException will occur

  •  

    CBT6.txt will be created inside e:\Unit4 directory and 

    d:\Java\ABC.txt will be removed

  •  

    Code will not compile

  •  

     CBT6.txt will be created inside e:\Unit4 directory but

    d:\Java\ABC.txt will not be removed

Java Programming Practice MCQs Codetantra. CBT-5

 




Post a Comment

0 Comments