Computer Knowledge

Java Core Classes and Threads

1,473 Questions

Java core classes and threads form the foundation of object oriented programming and are crucial for IT officer and programming exams. This includes concepts like string mutability, collections, and multithreading. Solve these questions to test your practical coding and theoretical knowledge.

String and StringBuffer classesThread execution methodsJava collections frameworkCharacter streams input outputVariable serialization rules

Java Core Classes and Threads Questions

Multiple choice technology programming languages
  1. int bMethod(int i) throws IOException{...}

  2. protected int bMethod(short s) throws FileNotFoundException{...}

  3. private String aMethod(byte b,short s) throws Exception{...}

  4. char bMethod(String s) throws RuntimeException{...}

  5. int bMethod(short sh){...}

Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

Overloading requires a change in the method's parameter list. Changing only the return type or exceptions does not overload a method. Option 514452 changes the parameter from short to int, and option 514455 changes it to String, making both valid overloads.

Multiple choice technology programming languages
  1. int aMethod(int i) throws IOException{...}

  2. protected int aMethod(int i) throws FileNotFoundException{...}

  3. public int aMethod(int i) throws Exception{...}

  4. public int aMethod(byte i) throws IOException{...}

  5. protected int aMethod(int i) throws IOException,FileNotFoundException{...}

Reveal answer Fill a bubble to check yourself
B,E Correct answer
Explanation

An overriding method can declare narrower or fewer checked exceptions than the overridden method, or none at all. It can also increase visibility (e.g., from protected to public). Thus, throwing FileNotFoundException (a subclass of IOException) or keeping the original signature are both valid.

Multiple choice technology web technology
  1. PsChainedException

  2. PsPageFlowException

  3. PsJspException

  4. PsServletException

Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

PS (Portal Server) utilizes PsChainedException and PsServletException to wrap and handle exceptions within its web tier. PsPageFlowException and PsJspException are not standard exception classes provided by the PS framework.

Multiple choice technology programming languages
  1. Float.POSITIVE_INFINITY

  2. Double.POSITIVE_INFINITY

  3. runtime Exception

  4. None of the Above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Math.max is overloaded for double and float. When comparing a float and a double, the float is promoted to a double. The result of Math.max(double, double) is a double, returning Double.POSITIVE_INFINITY.

Multiple choice technology programming languages
  1. Class A will not compile.

  2. Line 46 can throw the unchecked exception TestException.

  3. Line 45 can throw the unchecked exception TestException.

  4. Line 46 will compile if the enclosing method throws a TestException.

  5. Line 46 will compile if enclosed in a try block, where TestException

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

TestException extends Exception, making it a checked exception. Line 46 calls a.sayHello() which throws TestException, so this line must either be in a try-catch block or the enclosing method must declare 'throws TestException'. Option D correctly states that the enclosing method must throw the exception. Option E correctly states that a try block with TestException handling would allow compilation. Options B and C incorrectly call it unchecked - it's checked.

Multiple choice technology programming languages
  1. declare reset() using the synchronized keyword

  2. declare getName() using the synchronized keyword

  3. declare getCount() using the synchronized keyword

  4. declare the constructor using the synchronized keyword

  5. declare increment() using the synchronized keyword

Reveal answer Fill a bubble to check yourself
A,C,E Correct answer
Explanation

WMLScript is a lightweight, simplified version of JavaScript designed specifically for low-bandwidth mobile devices running WAP (Wireless Application Protocol).

Multiple choice technology programming languages
  1. An exception may be thrown at runtime.

  2. The code may run with no output, without exiting.

  3. The code may run with no output, exiting normally.

  4. The code may rum with output “A B A B C C “, then exit.

  5. The code may rum with output “A B C A B C A B C “, then exit.

  6. The code may ruin with output “A B C A A B C A B C “, then exit.

Reveal answer Fill a bubble to check yourself
E,F Correct answer
Explanation

Line 15 has a typo: 'public void ruin()' should be 'public void run()' since Thread's entry point is run(). This means the threads actually execute the default no-op run() method, doing nothing. However, the threads start successfully, and since start() is called twice, both threads may or may not execute add() operations before main exits. Options E and F describe possible outputs: if threads execute, they print sequences of 'A B C'; the exact order and repetition depend on thread scheduling, which is nondeterministic.

Multiple choice technology programming languages
  1. The output may be “r1 = 6, r2 = 14”.

  2. The output may be “r1 = 5, r2 = 15”.

  3. The output may be “r1 = 8, r2 = 12”.

  4. The code may run (and complete) with no output.

  5. The code may deadlock (without completing) with no output.

  6. M IllegalStateException or InterruptedException may be thrown at

Reveal answer Fill a bubble to check yourself
A,B,E Correct answer
Explanation

Three threads are started doing transfers: r1 loses 5, r2 gains 5; then r2 loses 2, r1 gains 2; then r1 loses 1, r2 gains 1. If all complete in order, r1=8, r2=12 (not an option). If threads interleave, r1=6,r2=14 or r1=5,r2=15 are possible. Deadlock can occur if two threads hold one Record each and wait for the other's Record (circular wait). Options A, B, E are correct. Option C's output is impossible given the transfer amounts. No IllegalStateException or InterruptedException is thrown.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The servlet lifecycle guarantees that init() is called only once when the servlet is first loaded, and destroy() is called only once when the servlet is being removed from service. The servlet container manages these calls and developers should never call them manually. This once-only behavior ensures proper initialization and cleanup of servlet resources.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

  5. E

  6. F

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To execute a class from a JAR file, the JAR must be in the CLASSPATH with its full path. Option C correctly places Poker.jar in /stuff/java and includes the full JAR path /stuff/java/Poker.jar in CLASSPATH. Wildcards like *.jar don't work in CLASSPATH.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

  5. E

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

When start() is called, the thread runs independently. The main thread prints 'End of method.' first, then the spawned thread prints 'run.' followed by the RuntimeException stack trace. Option D shows this. Option E is also possible since thread scheduling is non-deterministic - the spawned thread might complete before the main thread prints.

Multiple choice technology programming languages
  1. The application will crash.

  2. The code on line 29 will be executed.

  3. The code on line 5 of class A will execute.

  4. The code on line 5 of class B will execute.

  5. The exception will be propagated back to line 27.

Reveal answer Fill a bubble to check yourself
B,E Correct answer
Explanation

When NullPointerException occurs deep in the call chain (C.method3), it propagates up through B.method2() and A.method1() back to the try-catch block. The catch block executes (line 29 prints 'an error occurred'). Code after the throw in any method never runs, so lines 5 in A and B don't execute.

Multiple choice technology programming languages
  1. Micro Edition

  2. Mobile Edition

  3. Enterprise Edition

  4. Mono Edition

  5. Standard Edition

  6. Robust Edition

Reveal answer Fill a bubble to check yourself
A,C,E Correct answer
Explanation

Java historically had three main editions: Micro Edition (Java ME) for mobile/embedded devices, Standard Edition (Java SE) for desktop/general development, and Enterprise Edition (Java EE) for enterprise applications. Options A, C, and E represent these valid editions.