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
-
another_variable_name
-
Int
-
myIdentifier
-
variabletrackinghowmanyuserslogin
C
Correct answer
Explanation
Java variable names should follow lowerCamelCase, starting with a lowercase letter and capitalizing the first letter of each subsequent word. 'myIdentifier' is the correct choice, whereas 'another_variable_name' uses snake_case and 'Int' starts with an uppercase letter.
-
public static int main(String args)
-
public static void main(String arg)
-
public static final main()
-
public static void main(String[] args)
D
Correct answer
Explanation
The standard entry point for any standalone Java application is the main method, which must be declared as 'public static void main(String[] args)'. This signature specifies that the method is accessible from anywhere, does not require an instance of the class to run, returns nothing, and accepts an array of String arguments.
Which of the following is NOT a common refactoring technique?
-
Extract method.
-
Inline method.
-
Move method.
-
Rename method.
B
Correct answer
Explanation
Inlining a method involves copying the code from the method into the caller, effectively removing the method from the codebase. This is generally not considered a good refactoring technique as it can lead to code duplication and make it harder to maintain.
Which of the following is NOT a common refactoring pattern?
-
Extract method.
-
Move method.
-
Inline method.
-
Encapsulate field.
C
Correct answer
Explanation
Inlining a method involves copying the code from the method into the caller, effectively removing the method from the codebase. This is generally not considered a good refactoring pattern as it can lead to code duplication and make it harder to maintain.
Which of the following is NOT a common refactoring technique for improving the performance of a software application?
-
Loop unrolling.
-
Inlining.
-
Method overriding.
-
Data structure conversion.
C
Correct answer
Explanation
Method overriding is not typically used for improving performance. It is a technique used in object-oriented programming to allow a subclass to provide its own implementation of a method inherited from a superclass.
Which JavaScript method is used to add an event listener to an element?
-
addEventListener()
-
attachEvent()
-
onclick()
-
onmouseover()
A
Correct answer
Explanation
The addEventListener() method is used to add an event listener to an element, allowing you to specify the event type (such as 'click' or 'mouseover') and the function to be executed when the event occurs.
Which JavaScript method is used to remove an element from the DOM?
-
removeChild()
-
remove()
-
delete()
-
detach()
A
Correct answer
Explanation
The removeChild() method is used to remove an element from the DOM, allowing you to dynamically remove elements from the web page.
What is the purpose of a throw statement?
-
To raise an exception
-
To catch an exception
-
To handle an exception
-
To prevent an exception
A
Correct answer
Explanation
A throw statement is used to raise an exception. This means that it signals that an error has occurred, and that the program should stop executing.
What is the best way to handle errors that occur in a thread?
-
Use a try-catch block
-
Use a throw statement
-
Use an assert statement
-
All of the above
A
Correct answer
Explanation
The best way to handle errors that occur in a thread is to use a try-catch block.
Which of the following is not a good practice for error handling in a thread?
-
Use a try-catch block
-
Use a throw statement
-
Use an assert statement
-
Ignore errors
D
Correct answer
Explanation
Ignoring errors is not a good practice for error handling in a thread. It can lead to unexpected results and make it difficult to debug the program.
Which design pattern is commonly used in mobile applications to handle asynchronous tasks?
-
Singleton
-
Factory
-
Observer
-
AsyncTask
D
Correct answer
Explanation
AsyncTask is a design pattern commonly used in mobile applications to handle asynchronous tasks. It allows developers to perform long-running operations in the background without blocking the main thread. AsyncTask manages the lifecycle of the background task, including its execution, progress updates, and final result. This pattern helps maintain a responsive user interface while performing intensive tasks in the background.
Which design pattern is commonly used in mobile applications to handle user input validation?
-
Factory
-
Singleton
-
Observer
-
Strategy
D
Correct answer
Explanation
The Strategy design pattern is commonly used in mobile applications to handle user input validation. It allows developers to define a family of algorithms or strategies for performing a specific task, such as validating user input. The Strategy pattern provides a flexible and extensible way to handle different validation scenarios without modifying the core application logic. Developers can easily add or remove validation strategies as needed, making it easier to maintain and adapt the application to changing requirements.
Which design pattern is used to provide a way to add or remove objects from a collection dynamically?
-
Factory Method
-
Strategy
-
Observer
-
Composite
D
Correct answer
Explanation
The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.
Which design pattern is used to provide a way to undo or redo actions?
-
Command
-
Memento
-
Observer
-
Singleton
A
Correct answer
Explanation
The Command pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
What is the purpose of the fetch API in JavaScript?
-
To send HTTP requests
-
To receive HTTP responses
-
To parse JSON data
-
All of the above
D
Correct answer
Explanation
The fetch API in JavaScript can be used to send HTTP requests, receive HTTP responses, and parse JSON data.