Computer Knowledge
Java Core Classes and Threads
1,935 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
-
A method is called on that object
-
A member variable is set for that object
-
A text string is assigned to the object.
A
Correct answer
Explanation
In object-oriented programming, sending a message to an object is the mechanism by which an operation is invoked. This results in the execution of a method defined within that object's class.
-
try.....catch()
-
try.....except()
-
if.....except()
-
if....else.....
B
Correct answer
Explanation
Python uses the try and except blocks to handle exceptions during program execution.
-
must be initialized
-
cannot be deleted
-
cannot be changed
-
None of the above
C
Correct answer
Explanation
Immutability in Java means that once a String object is created, its internal state (the sequence of characters) cannot be changed.
-
the Java Virtual Machine
-
code in a try block
-
calls from a try block to other methods
-
All of the above
D
Correct answer
Explanation
Exceptions can be thrown by the JVM (e.g., NullPointerException), by explicit throw statements in code, or by methods called within a try block.
-
throws IoException, WidgetException
-
catch IOException, WidgetException
-
throws Exception, WidgetException
-
throws WidgetException. IOException
-
addMouseListener
-
addListener
-
addObjectListener
-
addActionListener
D
Correct answer
Explanation
In Java Swing, the ActionListener interface is specifically designed to handle action events, such as clicking a button. The addActionListener method registers an object to receive these events.
-
System.out()
-
out.write.Text()
-
System.out.println()
-
System.printText()
C
Correct answer
Explanation
System.out.println() is the standard method used to print text to the console in Java.
D
Correct answer
Explanation
The 'main' method is the entry point where the Java Virtual Machine begins execution of a program.
-
@Test
-
@After
-
@BeforeClass
-
@Before
D
Correct answer
Explanation
In JUnit 4, the @Before annotation is used on a method to indicate that it should be executed before each @Test method in the class. @BeforeClass runs once before any tests in the class, while @After runs after each test.
-
HashMap
-
ArrayMap
-
Hashtable
-
TreeMap
A,C,D
Correct answer
Explanation
HashMap, Hashtable, and TreeMap are all standard classes in the Java Collections Framework that implement the java.util.Map interface. ArrayMap is not a standard JDK class as it is part of Android utilities.
-
public void main(String args[])
-
public void static main(String args[])
-
public static void main(String args[])
-
public static void main()
C
Correct answer
Explanation
The standard entry point for a Java application is public static void main(String args[]) (or String[] args). It must be public so the JVM can access it, static so it can be called without instantiating the class, void because it returns no value, and must accept an array of Strings as arguments.
-
myArray.size
-
myArray.size()
-
myArray.length
-
myArray.length()
C
Correct answer
Explanation
In Java, arrays are objects that expose a public, final field named 'length' to store the number of elements. Because it is a field and not a method, you access it without parentheses, unlike the length() method used for Strings or the size() method used for Collections.
-
Collections
-
Throwable
-
List
-
Collection
D
Correct answer
Explanation
The Collection interface (java.util.Collection) is the root interface of the collection hierarchy, from which List, Set, and Queue inherit. Note that 'Collections' is a utility class containing static methods, not an interface.
-
String[] running = new String[10];
-
String[] running = String[10];
-
String[10] running = new String[];
-
String running = String[10];
A
Correct answer
Explanation
To declare and instantiate an array of ten Strings in Java, you use the syntax 'String[] running = new String[10];'. This correctly specifies the array type, variable name, and allocates space for 10 String references.
C
Correct answer
Explanation
The 'return' keyword is used in Java to immediately exit from a method and optionally pass back a value to the caller. The 'break' keyword is used to exit loops or switch statements, not methods.