0% found this document useful (0 votes)
66 views

Java MCQ

The default encoding for an OutputStreamWriter is the default encoding of the host platform. An OutputStreamWriter is used to write character streams to the underlying output stream using a specified character encoding. If no encoding is specified, the default encoding of the host platform is used.

Uploaded by

Deepika Vashisth
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Java MCQ

The default encoding for an OutputStreamWriter is the default encoding of the host platform. An OutputStreamWriter is used to write character streams to the underlying output stream using a specified character encoding. If no encoding is specified, the default encoding of the host platform is used.

Uploaded by

Deepika Vashisth
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

06. Which of the following is advantage of using JDBC connection pool?

Better performance///
Slow performance
Using more memory
Using less memory

10. Thin driver is also known as?


Type 3 Driver
Type-2 Driver
Type-4 Driver ///
Type-1 Driver

11. How constructor can be used for a servlet?


Initialization and Constructor function///
Constructor function
Setup() method
Initialization

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)

16. How can a servlet call a JSP error page?


When the servlet throws the exception, it will automatically be caught by the
calling JSP page
This capability is not supported
The servlet needs to forward the request to the specific error page URL. The
exception is passed along as an attribute named “javax.servlet.jsp.jspException”
The servlet needs to redirect the response to the specific error page, saving the
exception off in a cookie
View Answer
Answer : C

ServletRequest is used to read data from a client request.

19. Which of the following ways to create a servlet?


Using Servlet Interface
Using GenericServlet class
Using HttpServlet class
All of given
View Answer
Answer : D

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.

2. Can servlet class declare constructor with ServletConfig object as an argument?


a) True
b) False
View Answer

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.

5. Which method is used to get three-letter abbreviation for locale’s country in


servlets?
a) Request.getISO3Country()
b) Locale.getISO3Country()
c) Response.getISO3Country()
d) Local.retrieveISO3Country()
View Answer

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

When destroy() method of a filter is called?


a) The destroy() method is called only once at the end of the life cycle of a
filter

8. Which of the following is true about servlets?


a) Servlets execute within the address space of web server
b) Servlets are platform-independent because they are written in java
c) Servlets can use the full functionality of the Java class libraries
d) Servlets execute within the address space of web server, platform independent
and uses the functionality of java class libraries
View Answer

Answer: d

9. How is the dynamic interception of requests and responses to transform the


information done?
a) servlet container
b) servlet config
c) servlet context
d) servlet filter
View Answer
Answer: d
Explanation: Servlet has various components like container, config, context,
filter. Servlet filter provides the dynamic interception of requests
and responses to transform the information.

10. Which are the session tracking techniques?


i. URL rewriting
ii. Using session object
iii.Using response object
iv. Using hidden fields
v. Using cookies
vi. Using servlet object
a) i, ii, iii, vi
b) i, ii, iv, v
c) i, vi, iii, v
d) i, ii, iii, v
View Answer

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?

RuntimeException and Error class


Exception and VirtualMachineError class
Error and Exception class
IOException and VirtualMachineError class
Hide Answer Workspace
Answer: (c) Error and Exception 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.

What is the use of the intern() method?

It returns the existing string from memory

Which of the following is false?

The rt.jar stands for the runtime jar


It is an optional jar file
It contains all the compiled class files
All the classes available in rt.jar is known to the JVM
Hide Answer Workspace
Answer: (b) It is an optional jar file.

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.

Which of the following is a valid syntax to synchronize the HashMap?

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.

The volatile keyword is not a replacement of a synchronized block or method as it


does not remove the need for synchronization among the atomic actions.

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.

50) What is the default encoding for an OutputStreamWriter?

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

You might also like