250+ TOP MCQs on Networking – Datagrams and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Networking – Datagrams”.

1. Which of these is a bundle of information passed between machines?
a) Mime
b) Cache
c) Datagrams
d) DatagramSocket

Answer: c
Clarification: The Datagrams are the bundle of information passed between machines.

2. Which of these class is necessary to implement datagrams?
a) DatagramPacket
b) DatagramSocket
c) All of the mentioned
d) None of the mentioned

Answer: c
Clarification: None.

3. Which of these method of DatagramPacket is used to find the port number?
a) port()
b) getPort()
c) findPort()
d) recievePort()

Answer: b
Clarification: None.

4. Which of these method of DatagramPacket is used to obtain the byte array of data contained in a datagram?
a) getData()
b) getBytes()
c) getArray()
d) recieveBytes()

Answer: a
Clarification: None.

5. Which of these methods of DatagramPacket is used to find the length of byte array?
a) getnumber()
b) length()
c) Length()
d) getLength()

Answer: d
Clarification: getLength returns the length of the valid data contained in the byte array that would be returned from the getData () method. This typically is not equal to length of whole byte array.

6. Which of these class must be used to send a datagram packets over a connection?
a) InetAdress
b) DatagramPacket
c) DatagramSocket
d) All of the mentioned

Answer: d
Clarification: By using 5 classes we can send and receive data between client and server, these are InetAddress, Socket, ServerSocket, DatagramSocket, and DatagramPacket.

7. Which of these method of DatagramPacket class is used to find the destination address?
a) findAddress()
b) getAddress()
c) Address()
d) whois()

Answer: b
Clarification: None.

8. Which of these is a return type of getAddress() method of DatagramPacket class?
a) DatagramPacket
b) DatagramSocket
c) InetAddress
d) ServerSocket

Answer: c
Clarification: None.

9. Which API gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
a) getSocketAddress()
b) getAddress()
c) address()
d) none of the mentioned

Answer: a
Clarification: getSocketAddress() is used to get the socket address.

250+ TOP MCQs on Exception Handling and Answers

This set of Java Questions and Answers for Experienced people on “Exception Handling”.

1. Which of the following keywords is used for throwing exception manually?
a) finally
b) try
c) throw
d) catch

Answer: c
Clarification: “throw’ keyword is used for throwing exception manually in java program. User defined exceptions can be thrown too.

2. Which of the following classes can catch all exceptions which cannot be caught?
a) RuntimeException
b) Error
c) Exception
d) ParentException

Answer: b
Clarification: Runtime errors cannot be caught generally. Error class is used to catch such errors/exceptions.

3. Which of the following is a super class of all exception type classes?
a) Catchable
b) RuntimeExceptions
c) String
d) Throwable

Answer: d
Clarification: Throwable is built in class and all exception types are subclass of this class. It is the super class of all exceptions.

4. Which of the following operators is used to generate instance of an exception which can be thrown using throw?
a) thrown
b) alloc
c) malloc
d) new

Answer: d
Clarification: new operator is used to create instance of an exception. Exceptions may have parameter as a String or have no parameter.

5. Which of the following keyword is used by calling function to handle exception thrown by called function?
a) throws
b) throw
c) try
d) catch

Answer: a
Clarification: A method specifies behaviour of being capable of causing exception. Throws clause in the method declaration guards caller of the method from exception.

6. Which of the following handles the exception when a catch is not used?
a) finally
b) throw handler
c) default handler
d) java run time system

Answer: c
Clarification: Default handler is used to handle all the exceptions if catch is not used to handle exception. Finally is called in any case.

7. Which part of code gets executed whether exception is caught or not?
a) finally
b) try
c) catch
d) throw

Answer: a
Clarification: Finally block of the code gets executed regardless exception is caught or not. File close, database connection close, etc are usually done in finally.

8. Which of the following should be true of the object thrown by a thrown statement?
a) Should be assignable to String type
b) Should be assignable to Exception type
c) Should be assignable to Throwable type
d) Should be assignable to Error type

Answer: c
Clarification: The throw statement should be assignable to the throwable type. Throwable is the super class of all exceptions.

9. At runtime, error is recoverable.
a) True
b) False

Answer: b
Clarification: Error is not recoverable at runtime. The control is lost from the application.

Java for Experienced people, here is complete set on Multiple Choice Questions and Answers on Java.

contest

250+ TOP MCQs on Regular Expression and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Regular Expression”.

1. Which of the following is not a class of java.util.regex?
a) Pattern class
b) matcher class
c) PatternSyntaxException
d) Regex class

Answer: d
Clarification: java.util.regex consists 3 classes. PatternSyntaxException indicates syntax error in regex.

2. What is the significance of Matcher class for regular expression in java?
a) interpretes pattern in the string
b) Performs match in the string
c) interpreted both pattern and performs match operations in the string
d) None of the mentioned.

Answer: c
Clarification: macther() method is invoked using matcher object which interpretes pattern and performs match operations in the input string.

3. Object of which class is used to compile regular expression?
a) Pattern class
b) Matcher class
c) PatternSyntaxException
d) None of the mentioned

Answer: a
Clarification: object of Pattern class can represent compiled regular expression.

4. Which capturing group can represent the entire expression?
a) group *
b) group 0
c) group * or group 0
d) None of the mentioned

Answer: b
Clarification: Group 0 is a special group which represents the entire expression.

5. groupCount reports a total number of Capturing groups.
a) True
b) False

Answer: a
Clarification: groupCount reports total number of Capturing groups. this does not include special group, group 0.

6. Which of the following matches nonword character using regular expression in java?
a) w
b) W
c) s
d) S

Answer: b
Clarification: W matches nonword characters. [0-9], [A-Z] and _ (underscore) are word characters. All other than these characters are nonword characters.

7. Which of the following matches end of the string using regular expression in java?
a) z
b) \
c) *
d) Z

Answer: a
Clarification: z is used to match end of the entire string in regular expression in java.

8. What does public int end(int group) return?
a) offset from last character of the subsequent group
b) offset from first character of the subsequent group
c) offset from last character matched
d) offset from first character matched

Answer: a
Clarification: public int end(int group) returns offset from the last character of the subsequent group.

9. what does public String replaceAll(string replace) do?
a) Replace all characters that matches pattern with a replacement string
b) Replace first subsequence that matches pattern with a replacement string
c) Replace all other than first subsequence of that matches pattern with a replacement string
d) Replace every subsequence of the input sequence that matches pattern with a replacement string

Answer: d
Clarification: replaceAll method replaces every subsequence of the sequence that matches pattern with a replacement string.

10. What does public int start() return?
a) returns start index of the input string
b) returns start index of the current match
c) returns start index of the previous match
d) none of the mentioned

Answer: c
Clarification: public int start() returns index of the previous match in the input string.

250+ TOP MCQs on Hibernate and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Hibernate”.

1. Which of the following is not a core interface of Hibernate?
a) Configuration
b) Criteria
c) SessionManagement
d) Session

Answer: c
Clarification: SessionManagement is not a core interface of Hibernate. Configuration, Criteria, SessionFactory, Session, Query and Transaction are the core interfaces of Hibernate.

2. SessionFactory is a thread-safe object.
a) True
b) False

Answer: a
Clarification: SessionFactory is a thread-safe object. Multiple threads can access it simultaneously.

3. Which of the following methods returns proxy object?
a) loadDatabase()
b) getDatabase()
c) load()
d) get()

Answer: c
Clarification: load() method returns proxy object. load() method should be used if it is sure that instance exists.

4. Which of the following methods hits database always?
a) load()
b) loadDatabase()
c) getDatabase()
d) get()

Answer: d
Clarification: get() method hits database always. Also, get() method does not return proxy object.

5. Which of the following method is used inside session only?
a) merge()
b) update()
c) end()
d) kill()

Answer: b
Clarification: update() method can only be used inside session. update() should be used if session does not contain persistent object.

6. Which of the following is not a state of object in Hibernate?
a) Attached()
b) Detached()
c) Persistent()
d) Transient()

Answer: a
Clarification: Attached() is not a state of object in Hibernate. Detached(), Persistent() and Transient() are the only states in Hibernate.

7. Which of the following is not an inheritance mapping strategies?
a) Table per hierarchy
b) Table per concrete class
c) Table per subclass
d) Table per class

Answer: d
Clarification: Table per class is not an inheritance mapping strategies.

8. Which of the following is not an advantage of using Hibernate Query Language?
a) Database independent
b) Easy to write query
c) No need to learn SQL
d) Difficult to implement

Answer: d
Clarification: HQL is easy to implement. Also, to implement it HQL it is not dependent on a database platform.

9. In which file database table configuration is stored?
a) .dbm
b) .hbm
c) .ora
d) .sql

Answer: b
Clarification: Database table configuration is stored in .hbm file.

10. Which of the following is not an advantage of Hibernate Criteria API?
a) Allows to use aggregate functions
b) Cannot order the result set
c) Allows to fetch only selected columns of result
d) Can add conditions while fetching results

Answer: b
Clarification: addOrder() can be used for ordering the results.

Advanced 250+ TOP MCQs on JSP Elements and Answers

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “JSP Elements”.

1. Which one of the following is correct for directive in JSP?
a) <%@directive%>
b) <%!directive%>
c) <%directive%>
d) <%=directive%>

Answer: a
Clarification: Directive is declared as <%@directive%>.

2. Which of the following action variable is used to include a file in JSP?
a) jsp:setProperty
b) jsp:getProperty
c) jsp:include
d) jsp:plugin

Answer: c
Clarification: jsp:include action variable is used to include a file in JSP.

3. Which attribute uniquely identification element?
a) ID
b) Class
c) Name
d) Scope

Answer: a
Clarification: ID attribute is used to uniquely identify action element.

4. “out” is implicit object of which class?
a) javax.servlet.jsp.PrintWriter
b) javax.servlet.jsp.SessionWriter
c) javax.servlet.jsp.SessionPrinter
d) javax.servlet.jsp.JspWriter

Answer: d
Clarification: JspWriter object is referenced by the implicit variable out which is initialized automatically using methods in the PageContext object.

5. Which object stores references to the request and response objects?
a) sessionContext
b) pageContext
c) HttpSession
d) sessionAttribute

Answer: b
Clarification: pageContext object contains information about directives issued to JSP page.

6. What temporarily redirects response to the browser?
a)
b) <%@directive%>
c) response.sendRedirect(URL)
d) response.setRedirect(URL)

Answer: c
Clarification: response.sendRedirect(URL) directs response to the browser and creates a new request.

7. Which tag is used to set a value of a JavaBean?
a)
b)
c)
d)

Answer: a
Clarification: is used to set a value of a java.util.Map object.

8. Can and <%–comment–%> be used alternatively in JSP?
a) True
b) False

Answer: b
Clarification: is an HTML comment. <%–comment–%> is JSP comment.

9. Java code is embedded under which tag in JSP?
a) Declaration
b) Scriptlet
c) Expression
d) Comment

Answer: b
Clarification: Scriptlet is used to embed java code in JSP.

10. Which of the following is not a directive in JSP?
a) page directive
b) include directive
c) taglib directive
d) command directive

Answer: d
Clarification: command directive is not a directive in JSP.

250+ TOP MCQs on Character and Boolean Data Types and Answers

Java MCQs on Character and Boolean Datatypes of Java Programming Language.

1. What is the numerical range of a char data type in Java?
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535

Answer: d
Clarification: Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535.

2. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned

Answer: c
Clarification: Unicode defines fully international character set that can represent all the characters found in all human languages. Its range is from 0 to 65536.

3. Which of these values can a boolean variable contain?
a) True & False
b) 0 & 1
c) Any integer value
d) true

Answer: a
Clarification: Boolean variable can contain only one of two possible values, true and false.

4. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1

Answer: d
Clarification: First 0 to 127 character set in Unicode are same as those of ISO-LATIN-1 and ASCII.

5. Which one is a valid declaration of a boolean?
a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = ‘true’

Answer: c
Clarification: Boolean can only be assigned true or false literals.

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

  1.     class array_output {
  2.         public static void main(String args[]) 
  3.         {    
  4.             char array_variable [] = new char[10];
  5. 	    for (int i = 0; i < 10; ++i) {
  6.                 array_variable[i] = 'i';
  7.                 System.out.print(array_variable[i] + "" );
  8.                 i++;
  9.             }
  10.         } 
  11.     }

a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned

Answer: a
Clarification: None.
output:

$ javac array_output.java
$ java array_output
i i i i i

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

  1.     class mainclass {
  2.         public static void main(String args[]) 
  3.         {
  4.             char a = 'A';
  5.             a++;
  6. 	    System.out.print((int)a);
  7.         } 
  8.     }

a) 66
b) 67
c) 65
d) 64

Answer: a
Clarification: ASCII value of ‘A’ is 65, on using ++ operator character value increments by one.
output:

$ javac mainclass.java
$ java mainclass
66

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

  1.     class mainclass {
  2.         public static void main(String args[]) 
  3.         {
  4.             boolean var1 = true;
  5. 	    boolean var2 = false;
  6. 	    if (var1)
  7. 	        System.out.println(var1);
  8. 	    else
  9. 	        System.out.println(var2);
  10.        } 
  11.     }

a) 0
b) 1
c) true
d) false

Answer: c
Clarification: None.
output:

$ javac mainclass.java
$ java mainclass
true

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

  1.     class booloperators {
  2.         public static void main(String args[]) 
  3.         {
  4.             boolean var1 = true;
  5. 	    boolean var2 = false;
  6. 	    System.out.println((var1 & var2));
  7.         } 
  8.     }

a) 0
b) 1
c) true
d) false

Answer: d
Clarification: boolean ‘&’ operator always returns true or false. var1 is defined true and var2 is defined false hence their ‘&’ operator result is false.
output:

$ javac booloperators.java
$ java booloperators
false

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

  1.     class asciicodes {
  2.         public static void main(String args[]) 
  3.         {
  4.             char var1 = 'A';
  5. 	    char var2 = 'a';
  6. 	    System.out.println((int)var1 + " " + (int)var2);
  7.         } 
  8.     }

a) 162
b) 65 97
c) 67 95
d) 66 98

Answer: b
Clarification: ASCII code for ‘A’ is 65 and for ‘a’ is 97.
output:

$ javac asciicodes.java
$ java asciicodes
65 97