23 MST CS F213 Solution Key

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

Birla Institute of Technology and Science Pilani

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

Section A (20 marks)


● Answer the following MCQ, each correct attempt will be awarded 2 marks (max marks 2x10= 20)
● Attempt by putting a tick mark 🗸 (only no other forms will be considered) on the correct option
● Each wrong attempt will lead to a negative marking of 0.5 marks

1 What will be the output of the following Java program? A 0


.
interface calculate { void cal(int item);} B 2
class display implements calculate{
C 4
int x;
public void cal(int item) D None of the
mentioned
{ x = item * item; }
}
class interfaces
{
public static void main(String args[])
{
display arr = new display;
arr.x = 0;
arr.cal(2); System.out.print(arr.x);
}
}

2 What type of variable can be A public static


. defined in an interface?
B private final

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();
}
}

4 Which statement is true about Java? A Java is a sequence-dependent programming language


.
B Java is a code dependent programming language

C Java is a platform-dependent programming language.

D Java is a platform-independent programming language

5 What will be the output of the following code: A 32


. class increment {
public static void main(String args[]) B 24
{
C 27
int g = 3;
System.out.print(++g * 8); D 36
}
}
6 What will be the output of the following A The program does not compile because this
. code? cannot be referenced in a static method

public class Test implements Runnable{


public static void main(String[] args) { B The program compiles fine, but it does not
print anything because t does not invoke
Thread t=new Thread(this);
the run() method
t.start();
}
public void run() { C The program compiles and runs fine and
System.out.println("test"); displays test on the console.
}
}
D None of the above

7 The functionality of multiple inheritance can be implemented in Java by A Only I


. I. Extending one class and implementing multiple interfaces.
II. Implementing multiple interfaces. B Only III
III. Extending multiple classes and interfaces.
C I & II
IV. Extending multiple classes and one interface
D II & III

8 What is output of the below java code? A 0


. interface X{ int i = 5;}
B 5
class Y implements X
{ C 10
void f()
D Compilation
{
error
i = 10;
System.out.println("i="+i);
}
}
public class Main {

public static void main(String[] args) {


Y obj = new Y();
obj.f();
}
}
9 Which method of base class X, the derived class Y A m1( )
. cannot override?
class X { B m2( )
final public void m1() {
C Both m1( ) and m2( ) can override
}
public void m2() { D No method can overrride
}
}
class Y extends X {

// 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.*;

//custom exception to validate login credentials


class InvalidCredentialsException extends Exception {
//member variable to store our custom message
String msg;
InvalidCredentialsException(String msg) {
//passing the parameter to the super class constructor
super(msg);
this.msg=msg;
}

//overriding with our custom message


@Override
public String toString() {
return msg;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String id = sc.next();
String password = sc.next();
try {
if(id!="user1" || password!="1234"){
throw new InvalidCredentialsException("no such user with username - "+id);
}
}
catch(InvalidCredentialsException ex) {
//calls override toString() method
System.out.println(ex);
//prints message passed to the super constructor
System.out.println(ex.getMessage());
}
}
}

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));

System.out.print("Enter the Limit for fabonacci: ");

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();
}
}
}

3. Build a sequence diagram where a user can:


1. Log in to an application if their details are correct. If the details are correct they are redirected
to the main dashboard
2. If the details are incorrect they are redirected to the registration page where their details are
then checked to see if a user is already registered under the entered email. If not already
registered a user is created and they are redirected to the main dashboard
3. if already registered then a "username or password incorrect" message is shown [10 marks]

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]);
}
}
}

You might also like