Java MCQ
Java MCQ
Better performance///
Slow performance
Using more memory
Using less memory
13. Which of the following is/are steps for the servlet life cycle?
Servlet class is loaded
Servlet instance is created
init,Service,destroy method is invoked
All of the mentioned///
15. Which of the following code is used to get an attribute in a HTTP Session
object in servlets?
session.alterAttribute(String name)
session.updateAttribute(String name)
session.getAttribute(String name)///
session.setAttribute(String name)
20. Which component is used to compile, debug and execute the java program?
JIT
JDK
JVM
JRE
View Answer
Answer : B
Explanation: 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 so option B is correct.
Answer: b
Explanation: ServletConfig object is created after the constructor is called and
before init() is called.
So, servlet init parameters cannot be accessed in the constructor.
Answer: a
Explanation: Each country is usually denoted by a 3 digit code.ISO3 is the 3 digit
country code.
6. Which of the following code retrieves the body of the request as binary data?
a) DataInputStream data = new InputStream()
b) DataInputStream data = response.getInputStream()
c) DataInputStream data = request.getInputStream()
d) DataInputStream data = request.fetchInputStream()
View Answer
Answer: c
Explanation: InputStream is an abstract class. getInputStream() retrieves the
request in binary data
Answer: d
Answer: b
Explanation: URL rewriting, using session object, using cookies, using hidden
fields are session tracking techniques.
8) What is the return type of the hashCode() method in the Object class?
Object
int ////
long
void
For every long literal to be recognized by Java, we need to add L character at the
end of the expression. It can be either uppercase (L) or
lowercase (l) character. However, it is recommended to use uppercase character
instead of lowercase because the lowercase (l) character is hard to distinguish
from the uppercase (i) character.
For example,
Lowercase l: 0x466rffl
Uppercase L: 0nhf450L
The getName() method of the Class class returns the name (as String) of the entity
(class, interface) represented by this Class object. It is a non-static method,
and available in the java.lang package.
The getClass() method of the Object class returns the runtime class of this object.
The intern() and toString() methods are of String class.
In which process, a local variable has the same name as one of the instance
variables?
Serialization
Variable Shadowing
Abstraction
Multi-threading
Hide Answer Workspace
Answer: (b) Variable Shadowing
Explanation: There are following reasons for considering a variable shadowing, they
are listed below:
When we define a variable in a local scope with a variable name same as the name of
a variable defined in an instance scope.
When a subclass declares a variable with the same name as of the parent class
variable.
When a method is overridden in the child class.
Hence, the correct answer is option (b).
Anonymous inner classes are the same as the local classes except that they don't
have any name. The main use of it is to override methods of classes or interfaces.
And the rest three options are false about the anonymous inner classes as it can
have both methods and objects. It does not have any fixed came name.
The Random class is available in the java.util package. An object of the Random
class is used to generate a series of pseudorandom numbers. And the object of this
class is a thread-safe and cryptographically insecure object. The Random class
provides a variety of methods that are used to create random numbers of type
integers, float, long, double, etc.
The nameless objects are basically referred to as anonymous objects. The anonymous
objects do not have any names. We can also say that, when an object is initialized
but is not assigned to any reference variable, it is called an anonymous object.
24) Which of these classes are the direct subclasses of the Throwable class?
When a String is created using a new operator, it always created in the heap
memory. Whereas when we create a string using double quotes,
it will check for the same value as of the string in the string constant pool. If
it is found, returns a reference of it else create a new string
in the string constant pool.
Explanation: The rt.jar stands for the runtime jar that comprises of all the
compiled core class files for the Java Runtime Environment.
It generally consists of classes like java.lang.String, java.lang.Object,
java.io.Exception, etc., and all packages and classes available
in the rt.jar are known to the JVM. The rt.jar is the mandatory jar file for every
core java application as it contains all the core classes
In java, the "\w" regex is used to match with a word character consists of [a-zA-
Z_0-9]. For example, \w+ matches one or more word character
that is same as ([a-zA-Z_0-9] +).
The regex \W, \s, and \S are used for a non-word character, a whitespace character,
and a non-whitespace character, respectively.
Hence, the \w regex is used for a word character.
The notify(), notifyAll(), and wait() are the methods of the Object class.
Map m = hashMap.synchronizeMap();
HashMap map =hashMap.synchronizeMap();
Map m1 = Collections.synchronizedMap(hashMap);
Map m2 = Collection.synchronizeMap(hashMap);
Hide Answer Workspace
Answer: (c) Map m1 = Collections.synchronizedMap(hashMap);
Which of the following modifiers can be used for a variable so that it can be
accessed by any thread or a part of a program?
global
transient
volatile
default
Hide Answer Workspace
Answer: (c) volatile
Explanation: In Java, we can modify the values of a variable with the help of a
reserved keyword known as volatile. It is a different way of making a class
thread-safe. Thread-safe means that the methods and objects of a class are
accessible by multiple threads at the same time.
Global is not a reserved keyword in Java. The transient and default are keywords in
Java, but they are not used for accessing a variable by a thread from
any part of the program
There are two types of I/O stream. One is a byte stream, and the other is the
character stream. The Byte stream is used to perform input or output 8-bit
(equals to 1 byte) Unicode bytes whereas, the Character stream is used to read or
write a 16-bit (equals to 2 bytes) Unicode character.
Therefore, a single operation of character stream performs two bytes read/ write at
a time.
UTF-8
Default encoding of the host platform
UTF-12
None of the above
Hide Answer Workspace
Answer: (b) Default encoding of the host platform