Skip to content

Questions

  1. What is a JAVA thread?
  2. Explain Java threads with code
  3. Draw and explain the thread life cycle with a diagram
  4. Write a Java code that takes an A and B 3x3 matrix from the user and add them C= [A+B]
  5. Write a Java code that takes an A and B 3x3 matrix from the user and multiplies them C= [A*B]
  6. Write a Java code that takes an A and B 3x3 matrix from the user and multiplies them C= [A-B]
  7. Modify the program to handle errors using exception handling.
java
import java.util.Scanner;
   public class SafeDivide {
     public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          System.out.print("Enter numerator: ");
          int n = sc.nextInt();
          System.out.print("Enter denominator: ");
          int d = sc.nextInt();
          System.out.println("Quotient: " + (n / d));
          sc.close();
     }
 }
java
import java.util.Scanner;
   public class ArrayAccessTest {
     public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int[] arr = {70, 10, 20, 30, 50};
          System.out.print("Enter an index (0-4): ");
          int idx = sc.nextInt();
          System.out.println("Value: " + arr[idx]);
          sc.close();
     }
 }
java
public class FileReadTest {
  public static void main(String[] args) throws Exception {
          File f = new File("data.txt");
          Scanner fileScanner = new Scanner(f);

          while (fileScanner.hasNextLine()) {
                    System.out.println(fileScanner.nextLine());
          }
          fileScanner.close();
     }
 }
  1. You have 7 string-type elements in an ArrayList. Use two threads to print them with a delay of 200 ms between prints, so the output interleaves. Keep the thread task class separate from the main class.
  2. You have 12 character-type elements in an ArrayList. Use two threads to print them; one thread prints only vowels and the other prints only consonants (skip non-letters if any). Keep the thread class separate from the object/main class.
  3. You have 10 integer-type elements in an ArrayList. Print them using a foreach loop in Java using two threads. Keep the thread class separate from the main object class.
  4. Suppose you want to create an Exam table with attributes: ExamID, CourseCode, ExamDate, DurationMinutes, and Venue. Write the Oracle SQL command to create the table with ExamID as primary key and DurationMinutes greater than 0. Write only the JSP execution code to insert an exam record and handle SQL exceptions properly. Use Java OOP and 3 threads to print all numbers from 1 to X, but Thread-1 prints multiples of 2, Thread-2 prints multiples of 3, and Thread-3 prints multiples of 5 where X is the last 3 digits of your ID.
  5. Suppose you want to create a Teacher table with attributes: TeacherID, Name, Email, Department, and Contact. Write the Oracle SQL command to create the table with TeacherID as primary key and Email as UNIQUE. Write only the JSP execution code to select and display all teachers from a given department using PreparedStatement. Use Java OOP and 3 threads to print all odd numbers from 1 to X where X is the last 3 digits of your ID.
  6. Suppose you want to create an Employee table with attributes: EmpID, EmpName, Salary, Designation, and Phone. Write the Oracle SQL command to create the table with EmpID as primary key and Salary as NOT NULL. Write only the JSP execution code to update an employee’s salary by EmpID. Use Java OOP and 3 threads to print all prime numbers from 1 to X where X is the last 3 digits of your ID.
  7. An online learning platform wants to manage courses and students by creating a base class Course with private attributes courseId, courseName, instructor, and isAvailable, including methods to access them and a method enrollStudent() that marks the course as occupied. Two subclasses, VideoCourse and TextCourse, should extend Course, and an interface AssessmentPolicy should define a method calculateScore(int totalMarks, int obtainedMarks), where VideoCourse calculates scores normally and TextCourse adds a 5% bonus. In a CourseSystem class, demonstrate enrolling students and calculating their scores.
  8. A Bank Account Management System requires the creation of a base class Account with private attributes accountNo, accountHolder, balance, along with an appropriate constructor and getter methods. The class should include a method deposit(double amount) to add money to the balance, and a method displayInfo() to show the account number and holder name. Two subclasses, SavingsAccount and CheckingAccount, should extend the Account class, and both must implement an interface Transaction containing the method performTransaction(double amount). The performTransaction() method should attempt to withdraw money from the account while handling any InsufficientFundsException or other arithmetic errors gracefully, displaying an appropriate error message instead of terminating the program. The subclasses should override displayInfo() to specify the type of account (Savings or Checking). A BankSystem class with the main method will create objects of both subclasses, invoke their displayInfo() and performTransaction() methods, and demonstrate the system’s functionality.
  9. A Course Enrollment System requires the creation of a base class Course with private attributes courseId, courseName, maxSeats, and enrolledStudents, along with a constructor and getter methods. The class should include a method enrollStudent() to increment the number of enrolled students, and a method displayInfo() to show the course ID and name. Two subclasses, OnlineCourse and OfflineCourse, should extend the Course class, and both must implement an interface EnrollmentPolicy containing the method register() which should attempt to enroll a student while handling any ArithmeticException or IllegalStateException if the course is full, displaying an appropriate error message instead of terminating the program. The subclasses should override displayInfo() to specify the type of course (Online or Offline). A CourseSystem class with the main method will create objects of both subclasses, invoke their displayInfo() and register() methods, and demonstrate the system’s functionality.
  10. Write a Java program to create two threads: one thread prints the first 10 Fibonacci numbers, and the other prints the first 10 prime numbers. Use the Thread class.
  11. Write a Java program to create two threads: one thread prints even numbers from 2–20, and the other prints odd numbers from 1–19. Use the Runnable interface.
  12. Write a Java program to create two threads: one thread prints numbers from 1–5, and the other prints letters from A–E.

Educational resources for IUS students.