National Institute of Technology, Kurukshetra
Mid Sem Exam-1 (CSE + M&C)
September 2024
CSPC-205, Object Oriented Programming using Java
Time: 50 min MM: 30
Answer allthe six questions.
1. (a) If there are more method calls than can be stored on the method-call stack, an error
known as a(n) OCCurS.
(b) If a method contains a local variable with the same name as one of its class's fields, the
local variable the field in that method's scope. [1, 1]
2. Mention the output of the following code snippets with complete justification. [2x 4]
(a) public class Demo (b) String s1 ="coding";
{public static void main(String[] args) StringBuffer s2 = new StringBuffer(s1);
{[Link](c' +'o' +'d'+ 'e); } [Link]([Link](s2);
(c) Strings1 =new String("javaguides"); (d) String st = new String("javaguides");
String s2 = new String("javaguides"); String s2 = "javaguides";
[Link](s1 == s2); [Link](s1 == s2);
3. Write a Java program that reads a line of text, tokenizes the line using space characters as
delimiters and outputs only those words beginning with the letter "b". [4]
4. Write an application that inputs five numbers, each between 10 and 100, inclusive. As each
number is read, display it only if it's not a duplicate of a number already read. Provide for the
"worst case," in which all five numbers are different. Use the smallest possible array to solve
this problem. Display the complete set of unique values input after the user enters each new
value. [4]
5 Design a default class named Myinteger that contains:
Aprivate int data field named 'value' that stores the int value represented by this object.
Aparametrized constructor that creates a Myinteger object for the specified int value.
IAgetter method that returns the int value.
Amethod isEven() that returns true if the value in this object is even.
A
static method isEven(Mylnteger) that returns true if the specified value is even.
Two methods equals(int) and equals(Mylnteger) that return true if the value in this object is
equal to the specified value.
The main method should be in aseparate class. [6]
6. Develop a Java program to play lottery game. The program randomly generates a lottery of a
two-digit number, prompts the user to enter a two-digit number, and determines whether the
user wins according to the following rules:
1. If the user input matches the lottery nuriber in the exact order, the award is
$10,000.
2. If all digits in the user input match alldigits in the lottery number, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $1,000.
Note that the digits of a two-digit number may be 0. If a number is less than 10, we assume
the number is preceded by a 0 to form atwo-digit number. For example, number 8 is treated
as 08 and number 0 is treated as 00 in the program.
The main method should be in a separate class. [6]
National Institute of Technology, Kurukshetra
Mid Sem Exam-2
October 2024
CSPC-205, Object Oriented Programming using Java
Time: 50 min MM: 20
Note: Answer all the questions. For all the solutions, main method should be in aseparate class.
1. Design a class named Time. The class contains:
The private data fields hour, minute, and second that represent a time with their getters.
A no-arg constructor that creates a Time object for the current time. (The values of the data
fields will represent the current time. )
Aconstructor that constructs a Time object with a specified elapsed time since midnight, January
1, 1970, in milliseconds. (The values of the data fields willrepresent this time.)
Aconstructor that constructsa Time object with the specified hour, minute, and second.
Amethod named setTime(long elapseTime) that sets a new time for the object using the elapsed
time.
For example, if the elapsed time is 555550000 milliseconds, the hour is 10, the minute is 19, and the
second is 10. Implement this class. Write a test program that creates two Time objects (using new
Time () and new Time(555550000) ) and displays their hour, minute, and second in the format
hour:minute:second. [4]
2. Design an interface named Colorable with a void method named howToColor(). Every class of a
colorable object must implement the Colorable interface. Design a class named Square that extends
GeometricObject and implements [Link] howToColor to display the message Color all
four sides. Write a test program that creates an array of five GeometricObjects and for each object,
display its area and invoke its howToColor method if it is colorable. (4]
3. Write a program for binaryToDecimal (String binaryString) method, which converts a
binary string into a decimal number. Implement this method to throw a Number Fornat Exception
if the string is not a binary string. [2]
4. Define a Triangle class with three sides. In a triangle, the sum of any two sides is greater than the
other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class, and
modify the constructor of the Triangle class to throw an llegalTriangleException object if a triangle
is created with sides that violate the rule. Implement the method public Triangle (double
sidel, double side2, double side3) throws IllegalTriangleException [3]
5. Write a method that returns the maximum value in an ArrayList of Integer wrapper elements. The
method returns null if the list is null or the list size is 0.
public static Integer max (ArrayList list)
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes
this method to return the largest number in the input.
[4]
6 (a) What is the use of printStackTrace method?
[1]
(b) Can you create an array of Objects where each object is an instance of a different class? If yes,
show how to do it?
[2]
National Institute of Technology Kurukshetra
END SEMESTER EXAMINATION December 2024
[Link]. IIl Semester (Computer Engineering /Al-ML / M&C)
Question Paper
Paper: Java Programming Course Code: CSPC-205
Maximum Marks: 50 Time Allowed: 3 hours
Total questions: 6 (attempt all) Total No. of pages: 2
1, (a) An array myarr is to be read from the keyboard.
(i) Sort this array using in-built Java library method.
(i) Now, reverse sort this array.
(ii) Next, write a Java method that returns True if a given array is sorted in
ascending order, returns False if it is sorted in reverse order.
(b) Describe the exception hierarchy in Java. Why are Runtime Exceptions not checked?
Write some example scenario when finally block is not executed. (4, 5)
2 (a) Assume that a vehicle registration number in India consists of two uppercase letters
followed by two digits then followed by two uppercase letters and finally followed by
four digits. Write a Java program to generate a vehicle registration number.
(b) Write a method that finds the number of occurrences of a specified character in a
string using the following header:
public static int count (String str, char a)
For example, count ("Welcome", 'e') returns 2.
Write aseparate Java class containing main method to test the above method that
prompts the user to enter a string followed by a character then displays the number of
OcCurrences of the character in the string. (4, 4)
3. (a) What willbe the output?
ArrayList<Integer> list = new ArrayList>):
[Link] (1) ;
[Link] (2) ;
[Link] (3) ;
list. remove (1) ;
System. [Link] (list) ;
(b) Following code is giving error. Why?
int J array = (3, 5, 95, 4, 15, 34, 3, 6, 5}:
ArrayList<Integer> list = new Arraylist<> (Arrays. asList (array)):
System. [Link] (list) ;
(c) A small airline has just purchased a computer for its new automated reservation
system. You have been asked to develop the new system in Java. Write a program
to assign seats on each flight of the airline (capacity: 10 seats in each flight)
The program should display the following alternatives: Please typel for
FirstClass and Please type2 for Economy. If the user types 1, your
Page 1 of 2
application should assign a seat in the tirst class section(1-5). If user types 2, your
application should assign a seat in the economy section (6-10).
Your Java application should then aisplay a boarding pass indicating the person's
seat number and its class. The methods for displaying the boarding pass and printing
the seating chart are in a different package and these methods have protected
visibility. Use 1D array of type Boolean type to represent the seating chart of the
plane. Initialize it to false indicating all the seats are empty. Change its value
accordingto assignment. Your application should never assign a seat that has been
already assigned. (1, 1, 6)
4. (a) An nxn matrix is called a positíve MarkoV matrix if each element is positive and
the sum of the elements in each column is 1. Write the following method to check
whether a matrix is a Markov matrix:
public static boolean isMarkovMatrix ( double [ ] ] m)
Write a test program that prompts the user to enter a 3x3 matrix of double values and
tests whether it is a Markov matrix.
(b) Write a Java class that uses String method region-Matches to compare twO
strings input by the user. The program should input the number of characters to be
compared and the starting index of the comparison. The program should state whether
the strings are equal. Ignore the case of the characters when performing the
comparison. (3, 4)
5. (a) Java does not have direct support for multiple inheritance. How to solve this issue?
Show an example code.
(b) Show the use of copy, shuffle and frequency methods of Collections class.
(b) Suppose 6, 10, 14, 18, 22 is stored in ArrayList in Java. Write statement
that will display a random number from this list. (3, 3, 1)
6. (a) (i) Explain different techniques tocreate a thread in Java.
(ii) Withthe help of example, explain the use of join () method in a Java thread.
(ii)How do you set a priority for a thread? What is the default priority?
(iv) Write a multithreaded program to that uses two threads to print random odd
and even numbers alternately.
(b) Given two Java sets of textbooks of the following subjects (MI", "Os", "CN"
"COA" ) and ("CN", COA" "DSA"i "DBMS") respectively. Findthe subjects
that are (1) only present in the first set; (2) only present in the second set; (3)
present in both sets. Before writing the logic, first clone the sets to preserve the
original sets from being changed by the set methods. (6, 5)
THE END
Page 2 of 2