Professional Documents
Culture Documents
CS-20004 (Oop) - CS End April 2024
CS-20004 (Oop) - CS End April 2024
Programme:B.Tech
Branch/Specialization:CSE, CSCE
Learning Course
levels as Outcomes
SECTION-A (Learning levels 1)
per (CO)
Bloom’s
taxonomy
1 Answer the following questions. [1 × 10]
.
(a) Find the output / error for the below code 1 1
with brief explanation.
class Test {
public static void main(String[] args) {
for(int i = 0; 1; i++) {
System.out.println("Hello");
break;
} }}
Ans: Compilation Error (Incompatible types:
int cannot be converted to boolean)
Evaluation Scheme: Award 0.5 for output and
0.5 for brief explanation.
(b) Find the output / error for the below code 1 1
with brief explanation.
class Point {
protected int x, y;
public Point(int _x, int _y)
{
x = _x;
y = _y;
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
} }
public class Main {
public static void main(String args[])
{
Point p = new Point();
System.out.println("x = " + p.x + ", y = " +
p.y);
}}
Ans: Compilation Error (Constructor Point in class Point
cannot be applied to given types)
Evaluation Scheme: Award 0.5 for output and
0.5 for brief explanation.
(c) Find output/Error for the below code with 1 3,4
brief explanation.
import java.io.*;
class Parent{
void msg()throws Exception {
System.out.println("parent class method");
} }
class TestExceptionChild extends Parent{
void msg()throws ArithmeticException {
System.out.println("child fclass method");
}
public static void main(String args[]){
Parent p = new TestExceptionChild4();
try {
p.msg();
}catch(Exception e) { }
} }
Ans: Compilation Error Or child fclass method
If constructor name is TestExceptionChild4()
Or If constructor name is TestExceptionChild( )
Evaluation Scheme: Award 1 mark for output
compilation error or output.
(d) Find the output / error for the below code 1 5
with brief explanation.
class Test {
public static void main(String args[ ]) {
System.out.println(10 + 20 + "KIIT");
System.out.println("KIIT" + 10 + 20);
} }
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
Ans: 30KIIT
KIIT1020
Evaluation Scheme: Award 0.5 mark for
“30KIIT” output and 0.5 for mark “KIIT1020”.
(e) Find the output/error for the below code 1 5
with brief explanation.
class STRING {
public static void main(String args[ ]) {
String s="DATA";
s.concat("SCIENCE");
System.out.println(s);
s=s.concat("ANALYTICS");
System.out.println(s);
StringBuffer x = new StringBuffer("KALINGA");
System.out.println("The capacity before
append is:"+x.capacity());
x.append("ODISHA");
System.out.println("The capacity after append
is:"+x.capacity());
}}
Ans: DATA
DATAANALYTICS
The capacity before append is: 23
The capacity after append is: 23
Can extend another class and implement Can only extend another interface
interfaces
Output: true
false
true
class Queue {
private int maxSize;
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
private int[] queueArray;
private int front;
private int rear;
private int size;
try {
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
queue.display();
queue.display();
queue.enqueue(60);
queue.enqueue(70);
queue.display();
} catch (QueueException e) {
System.out.println("Queue Exception: "
+ e.getMessage());
}
}
}
Evaluation Scheme: Award 2.5 marks if the
student has attempted to implement Queue.
Evaluator discretion to award more marks for
mostly correct answers and 5 marks for a fully
correct answer.
3 (a) Draw the thread life cycle and explain the [5] 2,3,4 4
. different states of a thread. What are the
different techniques available in Java for
achieving synchronization of threads? Discuss
with suitable examples.
class Counter {
private int count = 0;
class Counter {
private int count = 0;
private Object lock = new Object();
Evaluation Scheme: [2 + 3]
(b) What are the differences between function [5] 2,3 3
overriding and function overloading. Write a java
program to create an abstract class Bank having
an abstract method getRateofInterest(). Derive
three classes SBI, ICICI and HDFC from Bank
class. Now use dynamic method dispatch
concept and function overriding concept to
override getRateofInterest() of bank class with
derived class methods to display rate of interest
of different banks.
Function overloading Function overriding
1 It is performed within a class. It occurs in two classes -one inherits from another.
2 It is the example of compile time It is the example of run time polymorphism.
polymorphism.
3 In this case parameters must be different. In this case parameters must be same in both the classes.
abstract class Bank{
abstract void getRateofInterest();
}
class driver{
public static void main (String ar[]){
Bank ob;
ob= new SBI();
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
ob.getRateofInterest();
ob= new ICICI();
ob.getRateofInterest();
ob= new HDFC();
ob.getRateofInterest();
}
}
Evaluation scheme: [2 + 3]
4 (a) Draw picture of multiple inheritance through [5] 2,3,4 3
. interface. Explain with example program how
multiple inheritance is supported in java
through interfaces and not by classes. Explain
the rules for ambiguity resolution while
implementing multiple inheritance using
interfaces with suitable example.
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void
show(){System.out.println("Welcome");}
import java.util.*;
class Swap
{
public static void main(String args[])
{
// Declare two strings
String a = "Hello";
String b = "World";
4(b)2.
Input: str1 = “listen” str2 = “silent”
Output: “Anagram”
Explanation: All characters of “listen” and “silent” are the same.
Input: str1 = “gram” str2 = “arm”
Output: “Not Anagram”
import java.util.Arrays;
class ANAGRM {
public static void main(String[] args) {
String str1 = "Race";
String str2 = "Care";
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if(result) {
System.out.println(str1 + " and " + str2 + " are anagram.");
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
}
else {
System.out.println(str1 + " and " + str2 + " are not anagram.");
}
}
}
Evaluation scheme: Award 1 mark for one point comparison and 4(b)1. award 2 marks for reverse of two
strings or exchange of two strings, award 2 for 4(b)2. program.
5 (a) What are the different ways to create a user [5] 2,4,5 4,5
. defined thread? Write a program in Java to
create two user defined threads which performs
the following task respectively:
import java.util.*;
class T1 extends Thread
{
String str;
Scanner sc=new Scanner(System.in);
T1()
{
System.out.println("Task-1: Enter a
String");
str=sc.next();
}
public void run()
{
try
{
for(int i=0;i<(str.length()-
1);i++)
{
System.out.println("Task-
1:"+str.substring(0,i+1));
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Task-1
interrupted");
}
}
}
class T2 extends Thread
{
String[] str;
StringBuilder sb;
int n;
T2()
{
Scanner sc=new
Scanner(System.in);
System.out.println("Task-2: Enter
no of strings");
n=sc.nextInt();
str=new String[n];
for(int i=0; i<n; i++)
{
System.out.print("Enter String
"+(i+1)+":");
str[i]=sc.next();
}
}
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
public void run()
{
try
{
for(int i=0;i<n;i++)
{
sb=new StringBuilder(str[i]);
System.out.println("Task-
2:"+sb.reverse());
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Task-2
interrupted");
}
}
}
class Main
{
public static void main(String[] args)
{
T1 t1 = new T1();
T2 t2 = new T2();
t1.start();
t2.start();
}
}
Output:
Task-1: Enter a String
KIIT
Task-2: Enter no of strings
6
Enter String 1:KIIT
Enter String 2:SCE
Enter String 3:CSE
Enter String 4:IT
Enter String 5:CSSE
Enter String 6:CSCE
Task-2:TIIK
Task-1:K
Task-1:KI
Task-2:ECS
Task-1:KII
Task-2:ESC
Task-2:TI
Task-2:ESSC
Task-2:ECSC
Evaluation scheme: [1 + 2 + 2]
(b) Draw the Throwable class hierarchy. State the [5] 3,4,5 4
differences between checked and unchecked
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
exceptions. Write a Java Program to create an
user defined exception called
IllegalArgsException and throw the exception if
we pass the negative timeout value in
Thread.sleep() method otherwise sleep for given
time and display “KIIT University” five times.
}
else{
for (int i=0;i<5;i++){
try{
Thread.sleep (sleeptime);
}catch (InterruptedException e){}
System.out.println ("KIIT University");
}
}
}
}
Evaluation scheme: [1 + 1 + 3]
6 (a) Create a swing application in Java to create a [5] 4,5,6 6
. student’s registration page to participate in a
University event. The registration entities have
to be displayed on a label and corresponding
textbox must be available to enter the relevant
data. Buttons must be placed on the frame to
carry out different operations. The details of the
components to be present on the frame are:
Component List:
1. Name(Label & Text Box) :String
2. Roll No (Label & Text Box): Integer
3. CGPA (Label & Text Box): Float
4. Branch : Dropdown Menu (CSE, IT,
CSCE, CSSE)
5. Email Id(Label & Text Box): String
6. SUBMIT (Button): To submit the data
entered and display the data in the frame
on the labels.
7. RESET (Button): To clear all the entered
data from the textboxes.
8. CHANGE COLOR (Button): To change the
color from GREEN(default) to PINK
Validation Rule:
1. The length of the Roll No must be in the
range 7 to 8.
2. The CGPA can’t be greater than 10.0 and
KIIT-DU/2023/SOT/Spring End Semester Examination-2023
less than 6.0
3. The Email ID must be in the format
<string>@<string>.<string>
An exception must be thrown if any of the
validation rules are not satisfied.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Custom Exception class for invalid data entry
// Extending RuntimeException class to make a
unchecked exception
class InvalidData extends RuntimeException{
InvalidData(String message){
super(message);
}
}
// I have used anonymous inner class to
implement ActionListener to reduce the size of
code
// I have also used GridLayout to arrange
components, which was not part of syllabus,
// but I have taught in the class since layouts
make arrangements easier.
class RegistrationPage{
RegistrationPage(){
JFrame frame = new JFrame("Registration
Page");
frame.setSize(500, 500); // Size of frame in
pixels
frame.setVisible(true);
frame.getContentPane().setBackground(Color.GR
EEN);
frame.setDefaultCloseOperation(JFrame.EXIT_O
N_CLOSE);
// Validations
if(roll.getText().length() <= 6 ||
roll.getText().length() >= 9)
throw new InvalidData("Roll
Number should have 7-8 digits.");
if(!studentEmail.matches("^[A-Za-z0-
9+_.-]+@(.+)$"))
throw new InvalidData("Email
should be in valid format.");
frame.getContentPane().setBackground(Color.PI
NK);
}
});
}
}
class Driver{
public static void main(String[] args){
new RegistrationPage();
}
}
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b2=new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
Evaluation scheme: [2 + 3]
*****