23 MST CS F213 Solution Key
23 MST CS F213 Solution Key
Hyderabad Campus
Mid Sem Question Booklet for Object Oriented Programming
CS F213 (Even Sem 2022-23)
Exam type: Closed Book
Weightage: 35% Date: 17th March 2023
Full Marks: 70 Time: 90 minutes
C public final
D Static final
3 A 22
What will be the output of the following Java code?
.
class A B 33
{
public int i; C Compilation Error
private int j;
} D Runtime Error
class B extends A
{
void display()
{
super.j = super.i + 1; System.out.println(super.i + " "
+ super.j);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
// override?
}
10 What is the length of the applet box made in the following java A 20
. program?
B Compilation error
import java.awt.*;
C Runtime Error
import java.applet.*;
public class myapplet extends Applet D Default value
{
Graphic g;
g.drawstring("A Simple Applet", 20,20);
}
Solution Key:
1 2 3 4 5 6 7 8 9 10
A 🗸 🗸 🗸
B 🗸
C 🗸 🗸 🗸
D 🗸 🗸 🗸
Section B (50 marks)
● Write the codes for the following problems. No extra space is provided so think before you write.
● You may divide the space into two columns to utilize space judiciously
● System.out.println can be written as Sop(), public static void main as PSVM().
● Other than braces and semicolons, any error that may lead to a compilation error will be
penalized.
1. Explain the exception hierarchy with a diagram and 3-4 sentences of description [5 marks].
Solution:
Write a java program to implement user-defined exceptions for Validating Login Credentials
Consider a use case where we want to validate the login credentials entered by the user and
throw an exception with a specific error message or a specific status code to make the user
better understand the exception. We can define a custom exception for this case [10 marks].
Solution:
import java.util.*;
2. Explain the life cycle of a thread with a diagram and one line comment on different states. [3
marks]
Solution:
Complete the following program by inheriting the thread to print Fibonacci & reverse series [12
marks].
class MainThread
{
public static void main(String[] args)
{
try
{
Fibonacci fib = new Fibonacci();
fib.start();
fib.sleep(4000);
Reverse rev = new Reverse();
rev.start();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Solution:
import java.io.*;
class Fibonacci extends Thread
{
public void run()
{
try
{
int a=0, b=1, c=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println("\n=================================");
System.out.println("Fibonacci series:");
while (n>0)
{
System.out.print(c+" ");
a=b;
b=c;
c=a+b;
n=n-1;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Class Reverse extends Thread
{
public void run()
{
try
{
System.out.println("\n=================================");
System.out.println("\nReverse is: ");
System.out.println("=================================");
for (int i=10; i >= 1 ;i-- )
{
System.out.print(i+" ");
}
System.out.println("\n=================================\n\n");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Solution:
4. Create an AWT Application using Frame. The Application displays 7 Buttons by the name of the
seven days of the week. Whenever the user clicks a button then a motivational morning quote is
displayed in a Text Field provided for the same. The morning quotes are stored in a String Array
for the seven days of the week. For every Button pressed a separate quote should be displayed.
Care should be taken to close the Frame. Implement the event handling in a separate class
than the class that implements frame [10 marks]
Solution: /*
* Evaluation Rubrics:
* Correct Frame Implementation: 4 Marks
* Correct Array Implementation: 1 Mark
* Correct WindowListener Implementation: 1 Marks
* Correct ActionListener Implementation using 2nd Class: 4 Marks
* Note: In case of incorrect implementation,
* Partial marking is subjected to evaluator;s discretion.
* */
package awtapps;
import java.awt.*;
import java.awt.event.*;
public class MorningQuote extends Frame implements WindowListener {
TextField tf;
Button b1,b2,b3,b4,b5,b6,b7;
String quote[];
MorningQuote(){
quote = new String[7];
quote[0] = "Monday Quote";
quote[1] = "Tuesday Quote";
quote[2]= "Wednesday Quote";
quote[3] = "Thursday Quote";
quote[4] = "Friday Quote";
quote[5] = "Saturday Quote";
quote[6] = "Sunday Quote";
tf = new TextField();
b1 = new Button("Monday");
b2 = new Button("Tuesday");
b3 = new Button("Wednesday");
b4 = new Button("Thursday");
b5 = new Button("Friday");
b6 = new Button("Saturday");
b7 = new Button("Sunday");
add(tf); add(b1); add(b2);
add(b3); add(b4); add(b5);
add(b6); add(b7);
MQEventHandler ob = new MQEventHandler(this);
b1.addActionListener(ob);
b2.addActionListener(ob);
b3.addActionListener(ob);
b4.addActionListener(ob);
b5.addActionListener(ob);
b6.addActionListener(ob);
b7.addActionListener(ob);
addWindowListener(this);
setVisible(true);
setSize(500,500);
setLayout(new GridLayout(8,1));
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public static void main(String[] args) {
// TODO Auto-generated method stub
new MorningQuote();
}
}
public class MQEventHandler implements ActionListener{
MorningQuote mq;
MQEventHandler(MorningQuote ob){
mq = ob;
}
public void actionPerformed(ActionEvent ae) {
if(mq.b1.hasFocus()) {
mq.tf.setText(mq.quote[0]);
}
else if(mq.b2.hasFocus()) {
mq.tf.setText(mq.quote[1]);
}
else if(mq.b3.hasFocus()) {
mq.tf.setText(mq.quote[2]);
}
else if(mq.b4.hasFocus()) {
mq.tf.setText(mq.quote[3]);
}
else if(mq.b5.hasFocus()) {
mq.tf.setText(mq.quote[4]);
}
else if(mq.b6.hasFocus()) {
mq.tf.setText(mq.quote[5]);
}
else if(mq.b7.hasFocus()) {
mq.tf.setText(mq.quote[6]);
}
}
}