250+ TOP MCQs on Java.lang – Boolean Wrapper Advance and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Java.lang – Boolean Wrapper Advance”.

1. Which of these methods of Boolean wrapper returns boolean equivalent of an object.
a) getBool()
b) booleanValue()
c) getbooleanValue()
d) getboolValue()

Answer: b
Clarification: None.

2. Which of the following constant are defined in Boolean wrapper?
a) TRUE
b) FALSE
c) TYPE
d) All of the mentioned

Answer: d
Clarification: Boolean wrapper defines 3 constants – TRUE, FALSE & TYPE.

3. Which of these methods return string equivalent of Boolean object?
a) getString()
b) toString()
c) converString()
d) getStringObject()

Answer: b
Clarification: None.

4. Which of these methods is used to know whether a string contains “true”?
a) valueOf()
b) valueOfString()
c) getString()
d) none of the mentioned

Answer: a
Clarification: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.

5. Which of these class have only one field?
a) Character
b) Boolean
c) Byte
d) void

Answer: d
Clarification: Void class has only one field – TYPE, which holds a reference to the Class object for type void. We do not create an instance of this class.

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

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "true";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error

Answer: a
Clarification: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:

$ javac Output.java
$ java Output
true

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

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "true false true";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:

$ javac Output.java
$ java Output
false

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

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String str = "TRUE";
  6.             boolean x = Boolean.valueOf(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) Compilation Error
d) Runtime Error

Answer: a
Clarification: valueOf() returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time.
Output:

$ javac Output.java
$ java Output
true

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    String str = "true false";
  6.             boolean x = Boolean.parseBoolean(str);
  7.             System.out.print(x);
  8.         }
  9.     }

a) True
b) False
c) System Dependent
d) Compilation Error

Answer: b
Clarification: parseBoolean() Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Example: Boolean.parseBoolean(“True”) returns true.
Example: Boolean.parseBoolean(“yes”) returns false.
Output:

$ javac Output.java
$ java Output
false

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

  1.     class Output
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5. 	   String x = Boolean.toString(false);
  6.         }
  7.     }

a) True
b) False
c) System Dependent
d) Compilation Error

Answer: b
Clarification: toString() Returns a String object representing the specified boolean. If the specified boolean is true, then the string “true” will be returned, otherwise the string “false” will be returned.
Output:

$ javac Output.java
$ java Output
false

250+ TOP MCQs on Data Structures-Set and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Data Structures-Set”.

1. What is the default clone of HashSet?
a) Deep clone
b) Shallow clone
c) Plain clone
d) Hollow clone

Answer: b
Clarification: Default clone() method uses shallow copy. The internal elements are not cloned. A shallow copy only copies the reference object.

2. Do we have get(Object o) method in HashSet.
a) True
b) False

Answer: b
Clarification: get(Object o) method is useful when we want to compare objects based on the comparison of values. HashSet does not provide any way to compare objects. It just guarantees unique objects stored in the collection.

3. What does Collections.emptySet() return?
a) Immutable Set
b) Mutable Set
c) The type of Set depends on the parameter passed to the emptySet() method
d) Null object

Answer: a
Clarification: Immutable Set is useful in multithreaded environment. One does not need to declare generic type collection. It is inferred by the context of method call.

4. What are the initial capacity and load factor of HashSet?
a) 10, 1.0
b) 32, 0.75
c) 16, 0.75
d) 32, 1.0

Answer: c
Clarification: We should not set the initial capacity too high and load factor too low if iteration performance is needed.

5. What is the relation between hashset and hashmap?
a) HashSet internally implements HashMap
b) HashMap internally implements HashSet
c) HashMap is the interface; HashSet is the concrete class
d) HashSet is the interface; HashMap is the concrete class

Answer: a
Clarification: HashSet is implemented to provide uniqueness feature which is not provided by HashMap. This also reduces code duplication and provides the memory efficient behavior of HashMap.

6. What will be the output of the following Java code snippet?

  1. public class Test 
  2. {
  3. 	public static void main(String[] args) 
  4.         {
  5. 		Set s = new HashSet();
  6. 		s.add(new Long(10));
  7. 		s.add(new Integer(10));
  8. 		for(Object object : s)
  9.                 {
  10. 		    System.out.println("test - "+object);
  11. 		}
  12. 	}
  13. }

a)

   Test - 10
   Test - 10

b) Test – 10
c) Runtime Exception
d) Compilation Failure

Answer: a
Clarification: Integer and Long are two different data types and different objects. So they will be treated as unique elements and not overridden.

7. Set has contains(Object o) method.
a) True
b) False

Answer: a
Clarification: Set has contains(Object o) method instead of get(Object o) method as get is needed for comparing object and getting corresponding value.

8. What is the difference between TreeSet and SortedSet?
a) TreeSet is more efficient than SortedSet
b) SortedSet is more efficient than TreeSet
c) TreeSet is an interface; SortedSet is a concrete class
d) SortedSet is an interface; TreeSet is a concrete class

Answer: d
Clarification: SortedSet is an interface. It maintains an ordered set of elements. TreeSet is an implementation of SortedSet.

9. What happens if two threads simultaneously modify TreeSet?
a) ConcurrentModificationException is thrown
b) Both threads can perform action successfully
c) FailFastException is thrown
d) IteratorModificationException is thrown

Answer: a
Clarification: TreeSet provides fail-fast iterator. Hence when concurrently modifying TreeSet it throws ConcurrentModificationException.

10. What is the unique feature of LinkedHashSet?
a) It is not a valid class
b) It maintains the insertion order and guarantees uniqueness
c) It provides a way to store key values with uniqueness
d) The elements in the collection are linked to each other

Answer: b
Clarification: Set is a collection of unique elements.HashSet has the behavior of Set and stores key value pairs. The LinkedHashSet stores the key value pairs in the order of insertion.

250+ TOP MCQs on Try & Catch and Answers

Java MCQs on try and catch in Java Programming Language.

1. What is the use of try & catch?
a) It allows us to manually handle the exception
b) It allows to fix errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned

Answer: d
Clarification: None.

2. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check

Answer: a
Clarification: try is used for the block that needs to checked for exception.

3. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check

Answer: b
Clarification: None.

4. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check

Answer: c
Clarification: None.

5. Which of these statements is incorrect?
a) try block need not to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything

Answer: d
Clarification: try must be followed by either catch or finally block.

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = b / a;
  10.                System.out.print("Hello");
  11.            }
  12.            catch(Exception e) 
  13.            {
  14.                System.out.print("World");
  15.            } 
  16.         }
  17.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: b
Clarification: None.
Output:

$ javac Output.javac
java Output
World

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = a / b;
  10.                System.out.print("Hello");
  11.            }
  12.            catch(Exception e) 
  13.            {
  14.                System.out.print("World");
  15.            } 
  16.         }
  17.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: a
Clarification: None.
Output:

$ javac Output.javac
java Output
Hello

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = b / a;
  10.                System.out.print("Hello");
  11.            } 
  12.         }
  13.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: d
Clarification: try must be followed by either catch or finally
Output:

$ javac Output.javac
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Syntax error, insert "Finally" to complete BlockStatements

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try 
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = a / b;
  10.                System.out.print("Hello");
  11.            }
  12.            finally 
  13.            {
  14.                System.out.print("World");
  15.            } 
  16.         }
  17.     }

a) Hello
b) World
c) HelloWOrld
d) Compilation Error

Answer: c
Clarification: finally block is always executed after try block, no matter exception is found or not.
Output:

$ javac Output.javac
java Output
HelloWorld

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

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.            try
  6.            {
  7.                int a = 0;
  8.                int b = 5;
  9.                int c = b / a;
  10.                System.out.print("Hello");
  11.            }
  12.            catch(Exception e) 
  13.            {
  14.                System.out.print("World");
  15.            } 
  16.            finally 
  17.            {
  18.                System.out.print("World");
  19.            } 
  20.         }
  21.     }

a) Hello
b) World
c) HelloWOrld
d) WorldWorld

Answer: d
Clarification: finally block is always executed after tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.
Output:

$ javac Output.javac
java Output
WorldWorld

250+ TOP MCQs on ComponentEvent, ContainerEvent & FocusEvent Class and Answers

Java MCQs on ComponentEvent, ContainerEvent & FocusEvent Classes in Java Programming Language.

1. Which of these events is generated when the size of an event is changed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent

Answer: a
Clarification: A ComponentEvent is generated when the size, position or visibility of a component is changed.

2. Which of these events is generated when the component is added or removed?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent

Answer: b
Clarification: A ContainerEvent is generated when a component is added to or removed from a container. It has two integer constants COMPONENT_ADDED & COMPONENT_REMOVED.

3. Which of these methods can be used to obtain the reference to the container that generated a ContainerEvent?
a) getContainer()
b) getContainerCommand()
c) getActionEvent()
d) getContainerEvent()

Answer: d
Clarification: None.

4. Which of these methods can be used to get reference to a component that was removed from a container?
a) getComponent()
b) getchild()
c) getContainerComponent()
d) getComponentChild()

Answer: b
Clarification: The getChild() method returns a reference to the component that was added to or removed from the container.

5. Which of these are integer constants of ComponentEvent class?
a) COMPONENT_HIDDEN
b) COMPONENT_MOVED
c) COMPONENT_RESIZE
d) All of the mentioned

Answer: d
Clarification: The component event class defines 4 constants COMPONENT_HIDDEN, COMPONENT-MOVED, COMPONENT-RESIZE and COMPONENT-SHOWN.

6. Which of these events is generated when computer gains or loses input focus?
a) ComponentEvent
b) ContainerEvent
c) FocusEvent
d) InputEvent

Answer: c
Clarification: None.

7. FocusEvent is subclass of which of these classes?
a) ComponentEvent
b) ContainerEvent
c) ItemEvent
d) InputEvent

Answer: a
Clarification: None.

8. Which of these methods can be used to know the type of focus change?
a) typeFocus()
b) typeEventFocus()
c) isTemporary()
d) isPermanent()

Answer: c
Clarification: There are two types of focus events – permanent and temporary. The isTemporary() method indicates if this focus change is temporary, it returns a Boolean value.

9. Which of these is superclass of ContainerEvent class?
a) WindowEvent
b) ComponentEvent
c) ItemEvent
d) InputEvent

Answer: b
Clarification: ContainerEvent is superclass of ContainerEvent, FocusEvent, KeyEvent, MouseEvent and WindowEvent.

contest

250+ TOP MCQs on Generics and Answers

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

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

advertisement

  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.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
Hello

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

advertisement

  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 <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

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

Answer: b
Clarification: None.
Output:

$ javac Output.javac
$ java Output
36

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

  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:

advertisement

$ javac Output.javac
$ java Output
Hello 36

4. 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 <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: d
Clarification: genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:

advertisement

$ javac Output.javac
$ java Output

5. 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 <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer: d
Clarification: generic stack object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:

$ javac Output.javac
$ java Output

6. Which of these Exception handlers cannot be type parameterized?
a) catch
b) throw
c) throws
d) all of the mentioned

Answer: d
Clarification: we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.

7. Which of the following cannot be Type parameterized?
a) Overloaded Methods
b) Generic methods
c) Class methods
d) Overriding methods

Answer: a
Clarification: Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.

contest

Advanced 250+ TOP MCQs on Application Lifecycle – Ant, Maven and Jenkins and Answers

This set of Advanced Java Multiple Choice Questions & Answers (MCQs) on “Application Lifecycle – Ant, Maven and Jenkins”.

1. Which of below is not a dependency management tool?
a) Ant
b) Maven
c) Gradle
d) Jenkins

Answer: d
Clarification: Jenkins is continuous integration system. Ant, Maven, Gradle is used for build process.

2. Which of the following is not a maven goal?
a) clean
b) package
c) install
d) debug

Answer: d
Clarification: clean, package, install are maven goals. Debug is used finding and resolving of defects.

3. Which file is used to define dependency in maven?
a) build.xml
b) pom.xml
c) dependency.xml
d) version.xml

Answer: b
Clarification: pom.xml is used to define dependency which is used to package the jar. POM stands for project object model.

4. Which file is used to specify the packaging cycle?
a) build.xml
b) pom.xml
c) dependency.xml
d) version.xml

Answer: a
Clarification: Project structure is specified in build.xml.

5. Which environment variable is used to specify the path to maven?
a) JAVA_HOME
b) PATH
c) MAVEN_HOME
d) CLASSPATH

Answer: c
Clarification: MAVEN_HOME should be set to the bin folder of maven installation.

6. Which of the below is a source code management tool?
a) Jenkins
b) Maven
c) Git
d) Hudson

Answer: c
Clarification: Source code management tools help is version control, compare different versions of code, crash management, etc. Git, SVN are popular source code management tools.

7. Can we run Junits as a part of Jenkins job?
a) True
b) False

Answer: a
Clarification: As a part of jenkins job, we can run junits, fitnesse, test coverage reports, call shell or bat scripts, etc.

8. Which command can be used to check maven version?
a) mvn -ver
b) maven -ver
c) maven -version
d) mvn -version

Answer: d
Clarification: mvn -version can be used to check the version of installed maven from command prompt.

9. Which of the following is not true for Ant?
a) It is a tool box
b) It provides lifecycle management
c) It is procedural
d) It doesn’t have formal conventions

Answer: b
Clarification: Ant doesn’t provide lifecycle management. Maven provides lifecycle.

10. Which maven plugin creates the project structure?
a) dependency
b) properties
c) archetype
d) execution

Answer: c
Clarification: Archetype is the maven plugin which creates the project structure.