0% found this document useful (0 votes)
5 views4 pages

AnswerkeyProg QP Template CAT3-Set A

The document is a Continuous Assessment Test (CAT-III) for Object Oriented Programming, covering various topics such as Java threading, exception handling, JDBC architecture, and GUI development using JavaFX and AWT. It includes programming questions, theoretical explanations, and sample code for practical implementations. The test is structured into parts with specific marks allocated for each question.

Uploaded by

jacksparrow38390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

AnswerkeyProg QP Template CAT3-Set A

The document is a Continuous Assessment Test (CAT-III) for Object Oriented Programming, covering various topics such as Java threading, exception handling, JDBC architecture, and GUI development using JavaFX and AWT. It includes programming questions, theoretical explanations, and sample code for practical implementations. The test is structured into parts with specific marks allocated for each question.

Uploaded by

jacksparrow38390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Reg. No.

Continuous Assessment Test- III [CAT - III]

Year & Section : II

Semester : III

Branch : AI&ML and AI&DS

Sub. Code :CS23312

Subject Name : Object Oriented Programming

QP Code : 243504

[Regulations:2023]

Date: 04/11/2024 Time: 90Min Marks: 50


Answer ALL Questions

Part A [6 x 2 = 12 Marks]

4.1 Write a program to print "Good morning" and "Welcome" continuously on the screen in Java using threads. B2 CO4
Answer
public class ContinuousPrint {
public static void main(String[] args) {
new Thread(() -> { while (true) { System.out.println("Good morning"); try { Thread.sleep(1000); } catch (Exception e) {} } }).start();
new Thread(() -> { while (true) { System.out.println("Welcome"); try { Thread.sleep(1000); } catch (Exception e) {} } }).start();
}
}
4.2 Demonstrate gerPriority() and setPriority() methods in Java threads. B2 CO4
Answer
Thread t1 = new Thread(() -> System.out.println("Priority: " + Thread.currentThread().getPriority()));
Thread t2 = new Thread(() -> System.out.println("Priority: " + Thread.currentThread().getPriority()));
t2.setPriority(Thread.MAX_PRIORITY); t1.start(); t2.start();
4.3 Write a java program to handle exception for divinding a number by zero. B2 CO4

Answer

try { System.out.println(10 / 0); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); }


4.4 Give the methods used for inter thread communication. A2 CO4
Answer
wait() - Makes the thread wait until notified.
notify() and notifyAll() - Wakes up one or all waiting threads.
4.5 What is the use of notify methods in multithreading? A2 CO4
Answer
The notify() method in multithreading wakes up a single thread waiting on the same object's monitor.
4.6 Explain Type-4 driver. A2 CO4
The Type-4 driver, also known as the thin driver, is a pure Java driver for JDBC that directly converts JDBC calls to the database-specific
protocol without intermediate layers.
Part B [1x13=13 Marks]

4.7 a i) What is a thread? Describe the complete life cycle of thread. A2 CO4
Answer B2
New --> Runnable --> Running --> Terminated
| |
V V
Waiting Blocked
ii) Write a Java program using the thread function yield(), stop() and sleep methods
Answer
Thread t = new Thread(() -> { for (int i = 1; i <= 5; i++) { System.out.println(i); if (i == 3) Thread.yield(); try
{ Thread.sleep(1000); } catch (Exception e) {} } });
t.start(); try { Thread.sleep(3000); t.stop(); } catch (Exception e) {}
[OR]
b i) Discuss about JDBC architecture and explain in detail. A2 CO4
Answer B2

JDBC (Java Database Connectivity) architecture consists of four main components:

ii) JDBC API: Provides Java interfaces and classes to interact with databases.
iii) JDBC Drivers: Bridge between Java applications and databases, translating Java calls to database-specific protocol.
iv) JDBC Driver Manager: Manages a list of database drivers and establishes a connection.
v) Database: The actual data storage system where the SQL queries are executed, and results are retrieved.

ii)Create a JavaFX application for a basic media player with play, pause and stop functionalities.
Answer
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class MediaPlayerApp extends Application {


@Override
public void start(Stage primaryStage) {
Media media = new Media("file:///path/to/your/media.mp4");
MediaPlayer mediaPlayer = new MediaPlayer(media);

Button playButton = new Button("Play");


Button pauseButton = new Button("Pause");
Button stopButton = new Button("Stop");

playButton.setOnAction(e -> mediaPlayer.play());


pauseButton.setOnAction(e -> mediaPlayer.pause());
stopButton.setOnAction(e -> mediaPlayer.stop());

Scene scene = new Scene(playButton, 300, 250);


primaryStage.setTitle("Basic Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {


launch(args);
}
}
Part A [6 x 2 = 12 Marks]

5.1 Write a program to print the names of all fonts on your system. B2 CO5
Answer
import java.awt.GraphicsEnvironment; public class FontList { public static void main(String[] args) { for (String font :
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) System.out.println(font); } }
5.2 Write a java program to implement Mouse Events. B2 CO5
Answer
import java.awt.event.*; import javax.swing.*; public class MouseEventExample { public static void main(String[] args) { JFrame frame =
new JFrame(); frame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse
Clicked at: " + e.getX() + ", " + e.getY()); } }); frame.setSize(300, 200); frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
5.3 Write a java swing program to find the sum of two numbers. B2 CO5
Answer
import javax.swing.*; import java.awt.event.*; public class SumCalculator { public static void main(String[] args) { JFrame frame = new
JFrame(); JTextField num1 = new JTextField(10), num2 = new JTextField(10); JButton sumButton = new JButton("Sum"); JLabel
resultLabel = new JLabel("Sum: "); sumButton.addActionListener(e -> resultLabel.setText("Sum: " + (Integer.parseInt(num1.getText()) +
Integer.parseInt(num2.getText())))); frame.add(num1); frame.add(num2); frame.add(sumButton); frame.add(resultLabel);
frame.setLayout(new java.awt.FlowLayout()); frame.setSize(300, 150); frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
5.4 Give the role of layout manager. Which layout is default in Java? Define Border layout. A2 CO5
Answer
A layout manager in Java controls the arrangement and sizing of components within a container, with the default layout being
FlowLayout. BorderLayout divides a container into five regions (North, South, East, West, and Center), where components are placed
based on the region specified.
5.5 List the various Key events supported by Java A2 CO5
Answer

KeyPressed: Triggered when a key is pressed down.


KeyReleased: Triggered when a key is released.
KeyTyped: Triggered when a key is typed (character input).
5.6 Express the different ways to create a GUI using frames in AWT. A2 CO5
Answer
In AWT, a GUI can be created using frames by either extending the Frame class and overriding its methods or by creating an instance of
the Frame class and adding components to it using its constructor or setter methods.

Part B [1x13=13 Marks]

5.7 a i) Describe the types of layout management in detail with an example. A2 CO5
Answer B2
FlowLayout: Arranges components in a left-to-right flow, wrapping to the next line when needed.

BorderLayout: Divides the container into five regions (North, South, East, West, Center).

GridLayout: Organizes components in a grid of rows and columns with equal-sized cells.

GridBagLayout: Allows complex component arrangements by specifying grid positions and sizes for each component.

ii) Develop a Java program to implement the following Create four check boxes. The initial state of the first box should be
in checked state. The status of each check box should be displayed. When we change the state of a check box, the status
should be displayed and updated
Answer
import javax.swing.*;
import java.awt.event.*;
public class CheckboxExample {
public static void main(String[] args) {
JFrame frame = new JFrame();
JCheckBox cb1 = new JCheckBox("Checkbox 1", true), cb2 = new JCheckBox("Checkbox 2"), cb3 = new
JCheckBox("Checkbox 3"), cb4 = new JCheckBox("Checkbox 4");
JLabel label = new JLabel();
cb1.addItemListener(e -> label.setText("Checkbox 1: " + (cb1.isSelected() ? "Checked" : "Unchecked")));
cb2.addItemListener(e -> label.setText("Checkbox 2: " + (cb2.isSelected() ? "Checked" : "Unchecked")));
cb3.addItemListener(e -> label.setText("Checkbox 3: " + (cb3.isSelected() ? "Checked" : "Unchecked")));
cb4.addItemListener(e -> label.setText("Checkbox 4: " + (cb4.isSelected() ? "Checked" : "Unchecked")));
frame.add(cb1); frame.add(cb2); frame.add(cb3); frame.add(cb4); frame.add(label);
frame.setLayout(new java.awt.FlowLayout());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
[OR]
b i) Summarize the component class and clearly explain its various methods. A2 CO5
Answer B2
setSize(int width, int height): Sets the dimensions of the component.

setLocation(int x, int y): Sets the position of the component on the container.

setVisible(boolean visibility): Controls whether the component is displayed.

repaint(): Requests the component to be redrawn, often after changes.

ii) Analyze and write a simple calculator using mouse events that restrict only addition, subtraction, multiplication and division.
Answer

import javax.swing.*;

import java.awt.event.*;

public class Calculator {

public static void main(String[] args) {

JFrame frame = new JFrame();

JButton add = new JButton("+"), sub = new JButton("-"), mul = new JButton("*"), div = new JButton("/");

JTextField display = new JTextField(10);

add.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { display.setText("Addition selected"); } });

sub.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { display.setText("Subtraction selected"); } });

mul.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { display.setText("Multiplication selected"); } });

div.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { display.setText("Division selected"); } });

frame.add(display); frame.add(add); frame.add(sub); frame.add(mul); frame.add(div);

frame.setLayout(new java.awt.FlowLayout());

frame.setSize(300, 200);

frame.setVisible(true);

You might also like