Java MCQ Set 1
1. Servlet are used to program which component in a web application?
a) client
b) server
c) tomcat
d) applet
Answer
Answer: b [Reason:] A servlet class extends the capabilities of servers that host applications which are accessed by way of a request-response programming model.
2. Which component can be used for sending messages from one application to another?
a) server
b) client
c) mq
d) webapp
Answer
Answer: c [Reason:] Messaging is a method of communication between software components or applications. MQ can be used for passing message from sender to receiver.
3. How are java web applications packaged?
a) jar
b) war
c) zip
d) both jar and war
Answer
Answer: d [Reason:] war are deployed on apache servers or tomcat servers. With Spring boot and few other technologies tomcat is brought on the machine by deploying jar.
4. How can we connect to database in a web application?
a) oracle sql developer
b) toad
c) JDBC template
d) mysql
Answer
Answer: c [Reason:] JDBC template can be used to connect to database and fire queries against it.
5. How can we take input text from user in HTML page?
a) input tag
b) inoutBufferedReader tag
c) meta tag
d) scanner tag
Answer
Answer: a [Reason:] HTML provides various user input options like input, radio, text, etc.
6. Which of the below is not a javascript framework for UI?
a) Vaadin
b) AngularJS
c) KendoUI
d) Springcore
Answer
Answer: d [Reason:] Springcore is not a javascript framework. It is a comprehensive programming and configuration model for enterprise applications based on java.
7. Which of the below can be used to debug front end of a web application ?
a) Junit
b) Fitnesse
c) Firebug
d) Mockito
Answer
Answer: c [Reason:] Firebug integrates with firefox and enables to edit, debug and monitor CSS, HTML and javascript of any web page.
8. What type of protocol is HTTP?
a) stateless
b) stateful
c) transfer protocol
d) information protocol
Answer
Answer: a [Reason:] HTTP is a stateless protocol. It works on request and response mechanism and each request is an independent transaction.
9. What does MIME stand for?
a) Multipurpose Internet Messaging Extension
b) Multipurpose Internet Mail Extension
c) Multipurpose Internet Media Extension
d) Multipurpose Internet Mass Extension
Answer
Answer: b [Reason:] MIME is an acronym for Multi-purpose Internet Mail Extensions. It is used for classifying file types over the Internet. It contains type/subtype e.g. application/msword.
10. What is the storage capacity of single cookie?
a) 2048 MB
b) 2048 bytes
c) 4095 bytes
d) 4095 MB
Answer
Answer: c [Reason:] Storage capacity of cookies is 4095 bytes/cookie.
Java MCQ Set 2
1. Which of these method can be used to make the main thread to be executed last among all the threads?
a) stop()
b) sleep()
c) join()
d) call()
Answer
Answer: b [Reason:] By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.
2. Which of these method is used to find out that a thread is still running or not?
a) run()
b) Alive()
c) isAlive()
d) checkRun()
Answer
Answer: c [Reason:] The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise.
3. What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY?
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256
Answer
Answer: c [Reason:] None.
4. Which of these method waits for the thread to treminate?
a) sleep()
b) isAlive()
c) join()
d) stop()
Answer
Answer: c [Reason:] None.
5. Which of these method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()
Answer
Answer: c [Reason:] The default value of priority given to a thread is 5 but we can explicitly change that value between the permitted values 1 & 10, this is done by using the method setPriority().
6. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need access to a shared resource
b) Its a process by which many thread are able to access same shared resource simultaneously
c) Its a process by which a method is able to access many different threads simultaneously
d) Its a method that allow to many threads to access any information require
Answer
Answer: a [Reason:] When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization
7. What is the output of this program?
-
class newthread extends Thread
-
{
-
newthread()
-
{
-
super("My Thread");
-
start();
-
}
-
public void run()
-
{
-
System.out.println(this);
-
}
-
}
-
class multithreaded_programing
-
{
-
public static void main(String args[])
-
{
-
new newthread();
-
}
-
}
a) My Thread
b) Thread[My Thread,5,main].
c) Compilation Error
d) Runtime Error
Answer
Answer: b [Reason:] Although we have not created any object of thread class still we can make a thread pointing to main method, we can refer it by using this.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main].
8. What is the output of this program?
-
class newthread extends Thread
-
{
-
Thread t;
-
newthread()
-
{
-
t = new Thread(this,"My Thread");
-
t.start();
-
}
-
public void run()
-
{
-
try
-
{
-
t.join()
-
System.out.println(t.getName());
-
}
-
catch(Exception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
-
class multithreaded_programing
-
{
-
public static void main(String args[])
-
{
-
new newthread();
-
}
-
}
a) My Thread
b) Thread[My Thread,5,main].
c) Exception
d) Runtime Error
Answer
Answer: d [Reason:] join() method of Thread class waits for thread being called to finish or terminate, but here we have no condition which can terminate the thread, hence code ‘t.join()’ leads to runtime error and nothing will be printed on the screen.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
9. What is the output of this program?
-
class newthread extends Thread
-
{
-
Thread t;
-
newthread()
-
{
-
t = new Thread(this,"New Thread");
-
t.start();
-
}
-
public void run()
-
{
-
System.out.println(t.isAlive());
-
}
-
}
-
class multithreaded_programing
-
{
-
public static void main(String args[])
-
{
-
new newthread();
-
}
-
}
a) 0
b) 1
c) true
d) false
Answer
Answer: c [Reason:] isAlive() method is used to check whether the thread being called is running or not, here thread is the main() method which is running till the program is terminated hence it returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
10. What is the output of this program?
-
class newthread extends Thread
-
{
-
Thread t1,t2;
-
newthread()
-
{
-
t1 = new Thread(this,"Thread_1");
-
t2 = new Thread(this,"Thread_2");
-
t1.start();
-
t2.start();
-
}
-
public void run()
-
{
-
t2.setPriority(Thread.MAX_PRIORITY);
-
System.out.print(t1.equals(t2));
-
}
-
}
-
class multithreaded_programing
-
{
-
public static void main(String args[])
-
{
-
new newthread();
-
}
-
}
a) true
b) false
c) truetrue
d) falsefalse
Answer
Answer: d [Reason:] This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
Java MCQ Set 3
1. Which of these interface abstractes the output of messages from httpd?
a) LogMessage
b) LogResponse
c) Httpdserver
d) httpdResponse
Answer
Answer: a [Reason:] LogMessage is a simple interface that is used to abstract the output of messages from the httpd.
2. Which of these class is used to create servers that listen for either local or remote client programs?
a) httpServer
b) ServerSockets
c) MimeHeader
d) HttpResponse
Answer
Answer: b [Reason:] None.
3. Which of these is a standard for communicating multimedia content over email?
a) http
b) https
c) Mime
d) httpd
Answer
Answer: c [Reason:] MIME is an internet standard for communicating multimedia content over email. The HTTP protocol uses and extends the notion of MIME headers to pass attribute pairs between HTTP client and server.
4. Which of these methods is used to make raw MIME formatted string?
a) parse()
b) toString()
c) getString()
d) parseString()
Answer
Answer: a [Reason:] None.
5. Which of these class is used for operating on request from the client to the server?
a) http
b) httpDecoder
c) httpConnection
d) httpd
Answer
Answer: d [Reason:] None.
6. Which of these method of MimeHeader is used to return the string equivalent of the values stores on MimeHeader?
a) string()
b) toString()
c) convertString()
d) getString()
Answer
Answer:b [Reason:] toString() does the reverse of parse() method, it is used to return the string equivalent of the values stores on MimeHeader.
7. What is the output of this program?
-
import java.net.*;
-
class networking
-
{
-
public static void main(String[] args) throws Exception
-
{
-
URL obj = new URL("https://www.distpub.com/javamcq");
-
URLConnection obj1 = obj.openConnection();
-
System.out.print(obj1.getContentType());
-
}
-
}
Note: Host URL is written in html and simple text.
a) html
b) text
c) html/text
d) text/html
Answer
Answer: d [Reason:] None.
Output:
$ javac networking.java
$ java networking
text/html
8. Which of these is an instance variable of class httpd?
a) port
b) cache
c) log
d) All of the mentioned
Answer
Answer: d [Reason:] There are 5 instance variables : port, docRoot, log, cache and stopFlag. All of them are private.
9. What is the output of this program?
-
import java.net.*;
-
class networking
-
{
-
public static void main(String[] args) throws MalformedURLException
-
{
-
URL obj = new URL("https://www.distpub.com/javamcq");
-
System.out.print(obj.toExternalForm());
-
}
-
}
a) distpub
b) distpub.com
c) www.distpub.com
d) https://www.distpub.com/javamcq
Answer
Answer: d [Reason:] to ExternalForm() is used to know the full URL of an URL object.
Output:
$ javac networking.java
$ java networking
https://www.distpub.com/javamcq
Java MCQ Set 4
1. Which one of the following is not an access modifier?
a) Public
b) Private
c) Protected
d) Void
Answer
Answer: d [Reason:] Public, private, protected and default are the access modifiers.
2. All the variables of class should be ideally declared as ?
a) private
b) public
c) protected
d) default
Answer
Answer: a [Reason:] The variables should be private and should be accessed with get and set methods.
3. Which of the following modifier means a particular variable cannot be accessed within the package?
a) private
b) public
c) protected
d) default
Answer
Answer: a [Reason:] Private variables are accessible only within the class.
4. How can a protected modifier be accessed?
a) accessible only within the class
b) accessible only within package
c) accessible within package and outside the package but through inheritance only
d) accessible by all
Answer
Answer: c [Reason:] The protected access modifier is accessible within package and outside the package but only through inheritance. The protected access modifier can be used with data member, method and constructor. It cannot be applied on the class.
5. What happens if constructor of class A is made private?
a) Any class can instantiate objects of class A
b) Objects of class A can be instantiated only within the class where it is declared
c) Inherited class can instantiate objects of class A
d) classes within the same package as class A can instantiate objects of class A
Answer
Answer: b [Reason:] If we make any class constructor private, we cannot create the instance of that class from outside the class.
6. All the variables of interface should be ?
a) default and final
b) default and static
c) public,static and final
d) protect, static and final
Answer
Answer: c [Reason:] Variables of an interface are public, static and final by default because the interfaces cannot be instantiated, final ensures the value assigned cannot be changed with the implementing class and public for it to be accessible by all the implementing classes.
7. What is true of final class?
a) Final class cause compilation failure
b) Final class cannot be instantiated
c) Final class cause runtime failure
d) Final class cannot be inherited
Answer
Answer: d [Reason:] Final class cannot be inherited. This helps when we do not want classes to provide extension to these classes.
8. How many copies of static and class variables are created when 10 objects are created of a class?
a) 1, 10
b) 10, 10
c) 10, 1
d) 1, 1
Answer
Answer: a [Reason:] Only one copy of static variables is created when a class is loaded. Each object instantiated has its own copy of instance variables.
9. Can a class be declared with protected modifier?
a) True
b) False
Answer
Answer: b [Reason:] Protected class member (method or variable) is like package-private (default visibility), except that it also can be accessed from subclasses. Since there is no such concept as ‘subpackage’ or ‘package-inheritance’ in Java, declaring class protected or package-private would be the same thing.
10. Which is the modifier when there is none mentioned explicitly?
a) protected
b) private
c) public
d) default
Answer
Answer: d [Reason:] Default is the access modifier when none is defined explicitly. It means the member (method or variable) can be accessed within the same package.
Java MCQ Set 5
1. Which of the following access specifiers can be used for an interface?
a) Protected
b) Private
c) Public
d) Public, protected, private
Answer
Answer: a [Reason:] Interface can have either public access specifier or no specifier.The reason is they need to be implemented by other classes.
2. Which of the following is correct way of implementing an interface A by class B?
a) class B extends A{}
b) class B implements A{}
c) class B imports A{}
d) None of the mentioned
Answer
Answer: b [Reason:] Concrete class implements interface. They can be instantiated.
3. All methods must be implemented of an interface.
a) True
b) False
Answer
Answer: a [Reason:] Concrete classes must implement all methods in an interface.Through interface multiple inheritance is possible.
4. What type of variable can be defined in an interface?
a) public static
b) private final
c) public final
d) static final
Answer
Answer: d [Reason:] variable defined in an interface is implicitly final and static. They are usually written in capital letters.
5. What does an interface contain?
a) Method definition
b) Method declaration
c) Method declaration and definition
d) Method name
Answer
Answer: b [Reason:] Interface contains only declaration of the method.
6. What type of methods an interface contain by default?
a) abstract
b) static
c) final
d) private
Answer
Answer: a [Reason:] By default, interface contains abstract methods.The abstract methods need to be implemented by concrete classes.
7. What will happen if we provide concrete implementation of method in interface?
a) The concrete class implementing that method need not provide implementation of that method
b) Runtime exception is thrown
c) Compilation failure
d) Method not found exception is thrown
Answer
Answer: c [Reason:] The methods of interfaces are always abstract. They provide only method definition.
8. What happens when constructor is defined for an interface?
a) Compilation failure
b) Runtime Exception
c) The interface compiles successfully
d) The implementing class will throw exception
Answer
Answer: a [Reason:] Constructor is not provided by interface as objects cannot be instantiated.
9. What happens when we access the same variable defined in two interfaces implemented by the same class?
a) Compilation failure
b) Runtime Exception
c) The JVM is not able to identify the correct variable
d) The interfaceName.variableName needs to be defined
Answer
Answer: d [Reason:] The JVM needs to distinctly know which value of variable it needs to use. To avoid confusion to the JVM interfaceName.variableName is mandatory.
10. Can “abstract” keyword be used with constructor,Initialization Block, Instance Initialization and Static Initialization Block?
a) True
b) False
Answer
Answer: b [Reason:] No, Constructor, Static Initialization Block, Instance Initialization Block and variables can not be abstract.