Unit 4-2mark

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

UNIT – IV – 2 mark Question and Answers

1. What is a thread in Java?


In Java, a thread is the smallest unit of execution within a process. It represents an
independent path of execution that can run concurrently with other threads. Java threads
are used to achieve parallelism and multitasking.

2. How can you create a thread in Java?


A thread can be created in Java by extending the Thread class or by implementing the
Runnable interface and passing an instance of your custom class to a Thread object.
For example,
1. extending Thread:
class MyThread extends Thread { /* thread logic */ },
2. implementing Runnable:
class MyRunnable implements Runnable { /* run() method logic */ }.

3. What is the difference between extending the Thread class and implementing the
Runnable interface for creating threads in Java?
When extending the Thread class, your class is tightly coupled with the thread behavior,
and you cannot extend any other class. When implementing the Runnable interface, your
class can still extend another class, and you can pass instances of your class to multiple
threads if needed, promoting better code organization and reusability.

4. How do you start a thread in Java?


A thread can be started in Java by calling the start() method on a Thread object. The
start() method initiates the execution of the thread, and it internally calls the run() method
that you've overridden in your custom class.

5. What is the run() method in a Java thread?


The run() method is a method you override when creating a custom thread by
implementing the Runnable interface or extending the Thread class. It contains the code
that defines the thread's behavior and logic. When the thread is started, the run() method
is executed in a separate thread of execution.

6. What is the main benefit of using threads in Java?


The main benefit of using threads in Java is achieving concurrent execution, which can
lead to improved performance and responsiveness in applications. Threads allow multiple
tasks to run simultaneously, making efficient use of CPU resources.

7. How can you synchronize threads in Java?


The thread can be synchronized in java using the synchronized keyword. You can apply
synchronized to methods or code blocks to ensure that only one thread can access the
synchronized code at a time, preventing race conditions and ensuring data consistency.
8. What is a race condition in multi-threaded programming?
A race condition in multi-threaded programming occurs when two or more threads access
shared data concurrently, and the final outcome depends on the order of execution. This
can lead to unpredictable and erroneous behavior in the program.

9. What is thread priority in Java?


Thread priority in Java is an integer value that indicates the relative importance or
urgency of a thread's execution. Thread priority values range from 1 (lowest priority) to
10 (highest priority). The Java Virtual Machine (JVM) uses thread priorities as a hint for
scheduling threads, but the actual behavior may vary depending on the platform and JVM
implementation.

10. What is the Stack class in Java?


The Stack class in Java is a legacy class that extends the Vector class. It represents a last-
in, first-out (LIFO) data structure, where elements are pushed onto the stack and popped
off the stack in reverse order. It is commonly used for tasks that involve tracking a
sequence of elements in a stack-like manner.

11. What is the Vector class in Java?


The Vector class in Java is a legacy class that represents a dynamic array-like data
structure. It is similar to an ArrayList but is synchronized, making it thread-safe. It can
dynamically resize itself to accommodate elements and is primarily used in situations
where synchronized access to a collection is required.

12. How do you push an element onto a Stack in Java?


To push an element onto a Stack in Java using the push() method. For example:
myStack.push(myElement);

13. How do you pop an element from a Stack in Java?


To pop an element from a Stack in Java using the pop() method. It removes and returns
the top element from the stack. For example: Object poppedElement = myStack.pop();

14. What is the main advantage of using a Vector in Java?


The main advantage of using a Vector in Java is that it provides synchronized access to
its elements, making it thread-safe. This ensures that multiple threads can safely
manipulate the vector without causing data corruption or race conditions.

15. What is the main disadvantage of using a Vector in Java?


16. The main disadvantage of using a Vector in Java is that it is less efficient than some other
collection classes, such as ArrayList, when it comes to performance, especially in multi-
threaded scenarios. The synchronization overhead can impact performance.

17. What is the recommended alternative to using a Stack in Java?


The recommended alternative to using a Stack in Java is to use the Deque interface with
the LinkedList class. The Deque interface provides methods for both stack-like (LIFO)
and queue-like (FIFO) operations, making it a versatile choice.
18. Why are Stack and Vector considered legacy classes in Java?
Stack and Vector are considered legacy classes in Java because they were part of the
original Java Collections Framework introduced in Java 1.0. With the introduction of
more modern collection classes like ArrayList and the enhancements in Java Collections
Framework in later versions of Java (such as Java 1.2 and onwards), these classes are
considered less efficient and less versatile compared to their newer counterparts.

19. What is the Random class in Java?


The Random class in Java is a utility class that provides methods for generating
pseudorandom numbers. It is often used for tasks requiring randomization, such as
simulations or games.

20. How do you create an instance of the Random class in Java?


Create an instance of the Random class by using its constructor, typically with no
arguments. For example: Random random = new Random();

21. What is the purpose of the Enumeration interface in Java?


The Enumeration interface in Java is used to iterate over a collection of objects, such as
elements in a legacy collection class like Vector or Hashtable. It defines two methods,
hasMoreElements() and nextElement(), which are used for iterating through elements.

22. What is the main limitation of the Enumeration interface in Java?


The main limitation of the Enumeration interface is that it is a read-only interface,
meaning it only provides a way to iterate through elements but does not support
modification or removal of elements. This makes it less versatile compared to iterators
introduced later in the Java Collections Framework.

23. What is the Calendar class in Java used for?


The Calendar class in Java is an abstract class that provides methods for manipulating
dates, times, and calendars. It is used for performing various date and time calculations,
formatting, and parsing operations.

24. How do you create an instance of the Calendar class in Java?


You cannot create an instance of the Calendar class directly using its constructor. Instead,
you use static factory methods like Calendar.getInstance() to obtain a Calendar instance
based on the default locale and timezone.

25. What is the Date class in Java used for?


The Date class in Java represents a specific point in time, typically to the millisecond
precision. It is used for working with dates and times, but it has limited functionality and
has been mostly replaced by the java.time package introduced in Java 8.

26. What is the main drawback of the Date class in Java?


The main drawback of the Date class in Java is that it lacks support for modern date and
time operations, such as timezone handling and formatting. It also does not provide a
clean way to perform calendar-related calculations.
27. What is the GregorianCalendar class in Java?
28. The GregorianCalendar class in Java is an implementation of the Calendar class that
follows the Gregorian calendar system, which is the widely used civil calendar system
today. It provides functionality for working with dates and times based on the Gregorian
calendar.

29. How does the GregorianCalendar class differ from the Calendar class?
30. The GregorianCalendar class is a specific implementation of the abstract Calendar class
that follows the Gregorian calendar system. It provides additional functionality and
precision for working with dates and times compared to the abstract Calendar class,
which serves as a base for various calendar implementations.

You might also like