250+ TOP MCQs on Type Interface and Answers

Java MCQs on Type interface in Java Programming Language.

1. Why are generics used?
a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs detectable at compile time
d) Generics add stability to your code by making more of your bugs detectable at runtime

Answer: c
Clarification: Generics add stability to your code by making more of your bugs detectable at compile time.

2. Which of these type parameters is used for a generic class to return and accept any type of object?
a) K
b) N
c) T
d) V

Answer: c
Clarification: T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

3. Which of these type parameters is used for a generic class to return and accept a number?
a) K
b) N
c) T
d) V

Answer: b
Clarification: N is used for Number.

4. Which of these is an correct way of defining generic class?
a) class name(T1, T2, …, Tn) { /* … */ }
b) class name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) class name{T1, T2, …, Tn} { /* … */ }

Answer: b
Clarification: The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, …, and Tn.

5. Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java?
a) Generics provide type safety by shifting more type checking responsibilities to the compiler
b) Generics and parameterized types eliminate the need for down casts when using Java Collections
c) When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes
d) All of the mentioned

Answer: c
Clarification: None.

6. Which of the following reference types cannot be generic?
a) Anonymous inner class
b) Interface
c) Inner class
d) All of the mentioned

Answer: a
Clarification: None.

7. What will be the output of the following Java program?

  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, java.util.List<Box<U>> boxes)
  4.         {
  5.            Box<U> box = new Box<>();
  6.            box.set(u);
  7.            boxes.add(box);
  8.         }
  9.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  10.         {
  11.             int counter = 0;
  12.             for (Box<U> box: boxes)
  13.             {
  14.                 U boxContents = box.get();
  15.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  16.                 counter++;
  17.             }
  18.         }
  19.         public static void main(String[] args)
  20.         {
  21.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  22.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  23.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  24.         }
  25.     }

a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Box #0 contains [10].

8. What will be the output of the following Java program?

  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, 
  4.         java.util.List<Box<U>> boxes)
  5.         {
  6.            Box<U> box = new Box<>();
  7.            box.set(u);
  8.            boxes.add(box);
  9.         }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes) 
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes) 
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("[" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(0), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 0
b) 1
c) [1]
d) [0]

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
[0]

9. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     public class genericstack <E> 
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. 	public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. 	}
  9. 	public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. 	}
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Hello 36

10. What will be the output of the following Java program?

  1.     public class BoxDemo 
  2.     {
  3.         public static <U> void addBox(U u, 
  4.         java.util.List<Box<U>> boxes) 
  5.         {
  6.            Box<U> box = new Box<>();
  7.            box.set(u);
  8.            boxes.add(box);
  9.         }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes) 
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }        
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 10
b) Box #0 [10]
c) Box contains [10]
d) Box #0 contains [10]

Answer: d
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Box #0 contains [10].

Advanced 250+ TOP MCQs on Client and Server and Answers

Advanced Java Multiple Choice Questions & Answers (MCQs) on “Client and Server”.

1. How does applet and servlet communicate?
a) HTTP
b) HTTPS
c) FTP
d) HTTP Tunneling

Answer: d
Clarification: Applet and Servlet communicate through HTTP Tunneling.

2. In CGI, process starts with each request and will initiate OS level process.
a) True
b) False

Answer: a
Clarification: A new process is started with each client request and that corresponds to initiate a heavy OS level process for each client request.

3. Which class provides system independent server side implementation?
a) Socket
b) ServerSocket
c) Server
d) ServerReader

Answer: b
Clarification: ServerSocket is a java.net class which provides system independent implementation of server side socket connection.

4. What happens if ServerSocket is not able to listen on the specified port?
a) The system exits gracefully with appropriate message
b) The system will wait till port is free
c) IOException is thrown when opening the socket
d) PortOccupiedException is thrown

Answer: c
Clarification: public ServerSocket() creates an unbound server socket. It throws IOException if specified port is busy when opening the socket.

5. What does bind() method of ServerSocket offer?
a) binds the serversocket to a specific address (IP Address and port)
b) binds the server and client browser
c) binds the server socket to the JVM
d) binds the port to the JVM

Answer: a
Clarification: bind() binds the server socket to a specific address (IP Address and port). If address is null, the system will pick an ephemeral port and valid local address to bind socket.

6. Which of the below are common network protocols?
a) TCP
b) UDP
c) TCP and UDP
d) CNP

Answer: c
Clarification: Transmission Control Protocol(TCP) and User Datagram Protocol(UDP) are the two common network protocol. TCP/IP allows reliable communication between two applications. UDP is connection less protocol.

7. Which class represents an Internet Protocol address?
a) InetAddress
b) Address
c) IP Address
d) TCP Address

Answer: a
Clarification: InetAddress represents an Internet Protocol address. It provides static methods like getByAddress(), getByName() and other instance methods like getHostName(), getHostAddress(), getLocalHost().

8. What does local IP address start with?
a) 10.X.X.X
b) 172.X.X.X
c) 192.168.X.X
d) 10.X.X.X, 172.X.X.X, or 192.168.X.X

Answer: d
Clarification: Local IP addresses look like 10.X.X.X, 172.X.X.X, or 192.168.X.X.

9. What happens if IP Address of host cannot be determined?
a) The system exit with no message
b) UnknownHostException is thrown
c) IOException is thrown
d) Temporary IP Address is assigned

Answer: b
Clarification: UnknownHostException is thrown when IP Address of host cannot be determined. It is an extension of IOException.

10. What is the java method for ping?
a) hostReachable()
b) ping()
c) isReachable()
d) portBusy()

Answer: c
Clarification: inet.isReachable(5000) is a way to ping a server in java.

250+ TOP MCQs on JDK-JRE-JIT-JVM and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “JDK-JRE-JIT-JVM”.

1. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: b
Clarification: JDK is a core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.

2. Which component is responsible for converting bytecode into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: a
Clarification: JVM is responsible to converting bytecode to the machine specific code. JVM is also platform dependent and provides core java functions like garbage collection, memory management, security etc.

3. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: d
Clarification: JRE is the implementation of JVM, it provides platform to execute java programs.

4. Which component is responsible to optimize bytecode to machine code?
a) JVM
b) JDK
c) JIT
d) JRE

Answer: c
Clarification: JIT optimizes bytecode to machine specific language code by compiling similar bytecodes at the same time. This reduces overall time taken for compilation of bytecode to machine specific language.

5. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language

Answer: a
Clarification: Java is called ‘Platform Independent Language’ as it primarily works on the principle of ‘compile once, run everywhere’.

6. Which of the below is invalid identifier with the main method?
a) public
b) static
c) private
d) final

Answer: c
Clarification: main method cannot be private as it is invoked by external method. Other identifier are valid with main method.

7. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js

Answer: b
Clarification: Java files have .java extension.

8. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js

Answer: a
Clarification: The compiled java files have .class extension.

9. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type

Answer: a
Clarification: The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.

10. What is use of interpreter?
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT

Answer: b
Clarification: Interpreters read high level language (interprets it) and execute the program. Interpreters are normally not passing through byte-code and jit compilation.

250+ TOP MCQs on The Object Class and Answers

Java MCQs on Object class of Java Programming Language.

1. Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class

Answer: b
Clarification: Object class is superclass of every class in Java.

2. Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()

Answer: c
Clarification: None.

3. Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned

Answer: c
Clarification: None.

4. Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final

Answer: d
Clarification: Declaring a class final implicitly declared all of its methods final, and makes the class inheritable.

5. Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned

Answer: a
Clarification: A abstract class is incomplete by itself and relies upon its subclasses to provide a complete implementation. If we declare a class final then no class can inherit that class, an abstract class needs its subclasses hence both final and abstract cannot be used for a same class.

6. Which of these class relies upon its subclasses for complete implementation of its methods?
a) Object class
b) abstract class
c) ArrayList class
d) None of the mentioned

Answer: b
Clarification: None.

7. What will be the output of the following Java program?

  1.     abstract class A 
  2.     {
  3.         int i;
  4.         abstract void display();
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             System.out.println(j);
  12.         }
  13.     }    
  14.     class Abstract_demo 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.j=2;
  20.             obj.display();    
  21.         }
  22.     }

a) 0
b) 2
c) Runtime Error
d) Compilation Error

Answer: b
Clarification: class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:

$ javac Abstract_demo.java
$ java Abstract_demo 
2

8. What will be the output of the following Java program?

  1.    class A 
  2.    {
  3. 	int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16.              A obj2 = new A();
  17. 	     System.out.print(obj1.equals(obj2));
  18.         }
  19.    }

a) false
b) true
c) 1
d) Compilation Error

Answer: a
Clarification: obj1 and obj2 are two different objects. equals() is a method of Object class, Since Object class is superclass of every class it is available to every object.
output:

$ javac Output.java
$ java Output
false

9. What will be the output of the following Java code?

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.              Object obj = new Object();
  6. 	     System.out.print(obj.getclass());
  7.         }
  8.     }

a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error

Answer: c
Clarification: None.
output:

$ javac Output.java
$ java Output
class java.lang.Object

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

  1.    class A 
  2.    {
  3.         int i;
  4. 	int j;
  5.         A() 
  6.         {
  7.             i = 1;
  8.             j = 2;
  9.         }
  10.    }
  11.    class Output 
  12.    {
  13.         public static void main(String args[])
  14.         {
  15.              A obj1 = new A();
  16. 	     System.out.print(obj1.toString());
  17.         }
  18.    }

a) true
b) false
c) String associated with obj1
d) Compilation Error

Answer: c
Clarification: toString() is method of class Object, since it is superclass of every class, every object has this method. toString() returns the string associated with the calling object.
output:

250+ TOP MCQs on Java.io Byte Streams and Answers

Java MCQs on java.io byte streams of Java Programming Language.

1. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned

Answer: a
Clarification: InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.

2. Which of these class is used to read and write bytes in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader

Answer: c
Clarification: None.

3. Which of these method of InputStream is used to read integer representation of next available byte input?
a) read()
b) scanf()
c) get()
d) getInteger()

Answer: a
Clarification: None.

4. Which of these data type is returned by every method of OutputStream?
a) int
b) float
c) byte
d) none of the mentioned

Answer: d
Clarification: Every method of OutputStream returns void and throws an IOExeption in case of errors.

5. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()

Answer: b
Clarification: None.

6. Which of these method(s) is/are used for writing bytes to an outputstream?
a) put()
b) print() and write()
c) printf()
d) write() and read()

Answer: b
Clarification: write() and print() are the two methods of OutputStream that are used for printing the byte data.

7. What will be the output of the following Java program? (Note: inputoutput.java is stored in the disk.)

  1.     import java.io.*;
  2.     class filesinputoutput 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             InputStream obj = new FileInputStream("inputoutput.java");
  7.             System.out.print(obj.available());
  8.         }
  9.     }

a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file

Answer: c
Clarification: obj.available() returns the number of bytes.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
1422

(Output will be different in your case)

8. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if(i == 0) 
  15.                    {
  16.             	       System.out.print((char)c); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB

Answer: a
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
abc

9. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c)); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

a) abc
b) ABC
c) ab
d) AB

Answer: b
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
ABC

10. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     	public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c));
  17.                        obj2.write(1); 
  18.             	   }
  19.                }
  20.                System.out.print(obj2);
  21.            }
  22.         }
  23.     }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa

Answer: d
Clarification: None.
Output:

$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

250+ TOP MCQs on Networking – httpd.java Class and Answers

Java MCQs on httpd.java of Java Programming Language.

1. Which of these methods of httpd class is used to read data from the stream?
a) getDta()
b) GetResponse()
c) getStream()
d) getRawRequest()

Answer: d
Clarification: The getRawRequest() method reads data from a stream until it gets two consecutive newline characters.

2. Which of these method of httpd class is used to get report on each hit to HTTP server?
a) log()
b) logEntry()
c) logHttpd()
d) logResponse()

Answer: b
Clarification: None.

3. Which of these methods are used to find a URL from the cache of httpd?
a) findfromCache()
b) findFromCache()
c) serveFromCache()
d) getFromCache()

Answer: c
Clarification: serveFromCatche() is a boolean method that attempts to find a particular URL in the cache. If it is successful then the content of that cache entry are written to the client, otherwise it returns false.

4. Which of these variables stores the number of hits that are successfully served out of cache?
a) hits
b) hitstocache
c) hits_to_cache
d) hits.to.cache

Answer: d
Clarification: None.

5. Which of these method of httpd class is used to write UrlCacheEntry object into local disk?
a) writeDiskCache()
b) writetoDisk()
c) writeCache()
d) writeDiskEntry()

Answer: a
Clarification: The writeDiskCache() method takes an UrlCacheEntry object and writes it persistently into the local disk. It constructs directory names out of URL, making sure to replace the slash(/) characters with system dependent seperatorChar.

6. What will be the output of the following Java program?

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws Exception 
  5.         {
  6.             URL obj = new URL("javamcq");
  7.             URLConnection obj1 = obj.openConnection();
  8.             int len = obj1.getContentLength();
  9.             System.out.print(len);
  10.         }
  11.     }

Note: Host URL is having length of content 127.
a) 126
b) 127
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: None.
Output:

$ javac networking.java
$ java networking 
127

7. Which of these method is used to start a server thread?
a) run()
b) start()
c) runThread()
d) startThread()

Answer: a
Clarification: run() method is called when the server thread is started.

8. Which of these method is called when http daemon is acting like a normal web server?
a) Handle()
b) HandleGet()
c) handleGet()
d) Handleget()

Answer: c
Clarification: None.