Skip to content

Unit-3 Questions

Multithreaded Programming

The Java Thread Model, The Main Thread, Creating a Thread, Creating Multiple Threads, Using isAlive() and join(), Thread Priorities, Synchronization, Inter-thread Communication, Suspending, Resuming, and Stopping Threads, Obtaining a Thread's State, Using Multithreading.

Part A: Core Concepts

  1. What is a thread? Explain the difference between a process and a thread, and compare multiprocessing with multithreading.

  2. Explain the Java Thread Model and the life cycle of a thread (New, Runnable, Running, Blocked, Terminated).

Part B: Creating and Managing Threads

  1. What are the two primary ways to create a thread in Java? Provide a simple program that creates and runs multiple threads.

  2. Explain the purpose of the isAlive() and join() methods for managing thread execution. Provide a code example.

  3. What are thread priorities and how are they set? what is default priority?

Part C: Synchronization

  1. What is thread synchronization and why is it necessary? Explain how it is achieved in Java using synchronized methods and synchronized blocks. Provide a code example.

Part D: Inter-thread Communication

  1. Explain how threads can communicate using the wait(), notify(), and notifyAll() methods.

  2. Write a program to implement the producer-consumer problem, demonstrating effective inter-thread communication.

Part E: Practical Application

  1. Write a Java program using two threads to print numbers from 1 to 10 in sequence (i.e., Thread 1 prints "1", Thread 2 prints "2", Thread 1 prints "3", and so on).

Enumerations and Autoboxing

Enumerations, Type Wrappers, Autoboxing.

Part A: Enumerations

  1. What is an enumeration (enum) in Java? Demonstrate its basic use with suitable constant values and explain the values() and valueOf() methods with a code example.

  2. Write a program to create a DayOfWeek enumeration and add a custom method isWorkDay() to it, demonstrating that an enum can function like a class. For workday it should return true and the call DayOfWeek.SUNDAY.isWorkDay() should return false.

Part B: Type Wrappers and Autoboxing

  1. What are Type Wrappers? Explain why they are necessary in Java (e.g., for use in collections) with an example.

  2. What are Autoboxing and Unboxing? Provide a code example that shows how this feature simplifies using primitive types with wrapper classes.

Generics

What are Generics?, A Simple Generics Example, A Generics Class with two Type Parameters, The General Form of a Generic Class.

  1. What are Generics in Java? Explain their purpose (e.g., type safety, eliminating casts) and provide the general form of a generic class.

  2. Write a program demonstrating a generic class that can work with different data types, such as Integer and String.

  3. How do you create a generic class with multiple type parameters? Provide a code example of a generic Pair class that can hold two different types of objects (e.g., a String and an Integer).

Made with ❤️ for students, by a fellow learner.