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));
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);
}
}
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);
}
}
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);
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);
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); }
}
}
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);
}
}
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);
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);
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);
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));
}
}
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); }
}
}
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);
}
}
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); }
}
}
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);
}
}
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);
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); }
}
}
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) + " ");
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 "));
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);
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);
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);
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());
}
}
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); }
}
}
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));
}
}
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);
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);
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);
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);
}
}
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);
}
}
Java Programming Practice MCQs Codetantra. CBT-5



0 Comments