0% found this document useful (0 votes)
205 views22 pages

OPPE REvision 1

Uploaded by

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

OPPE REvision 1

Uploaded by

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

BSCCS2005: Sep 2023 OPE1 Questions

with Test Cases and Solutions

Overview
The exam will be conducted in two sessions. In each session, a student will be presented
with 6 questions out of which 4 are graded and 2 are optional (challenging). Out of the
4 graded questions, he/she should answer 3 questions correctly, passing all the private test
cases to get a full score. In case they attempt all 4, only the best 3 scores will be considered.
We configure 2 copies of each type on the portal, and using a randomization script, every
student sees one copy of each type on the exam portal.
The challenging questions are not graded. The purpose of these questions is to engage stu-
dents who finish the other questions early.
1 Session 2
1.1 Session 2 Type 1 Copy Constructor
Problem Statement
In a college, Student s1 chooses a set of courses. Student s2 also chooses all the courses
chosen by s1 except the second course, in place of which s2 chooses another course. Write a
program that defines two classes Student and Admission. Define copy constructor to create
s2 from s1 such that changing the values of instance variables of either s2 or s1 does not
affect the other one. The code takes name of student s2 and the new course chosen by s2 as
input.

• Class Student has/should have the following members.

– Private instance variables String name and String[] courses to store name
and courses chosen respectively
– Define required constructor(s)
– Accessor methods getName( ) and getCourses(int) to get the name of the
student and the course at a specific index respectively.
– Mutator methods setName(String) and setCourses(int,String) to set the
name of the student and the course at a specific index respectively.

• Class Admission has method main that does the following.

– Two objects of Student s1 and s2 are created. s2 is created using s1


– name of Student s2 and second course chosen by s2 are updated by taking the
input
– Finally, name of s1, s2 and second course chosen by s1 and s2 are printed

What you have to do

• Define constructor(s) in class Student

Template Code

import java.util.*;
class Student{
String name;
String[] courses;
//***** Define constructor(s) here
public void setName(String n) {
name = n;
}
public void setCourses(int indx, String c) {

Page 2
courses[indx] = c;
}
public String getName() {
return name;
}
public String getCourses(int indx) {
return courses[indx];
}
}
public class Admission {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] courses = {"Maths", "DL","DSA","DC"};
Student s1 = new Student("Nandu", courses);
Student s2 = new Student(s1);
s2.setName(sc.next());
s2.setCourses(1,sc.next());
System.out.println(s1.getName() + ": "+ s1.getCourses(1));
System.out.println(s2.getName() + ": " + s2.getCourses(1));
}
}

Test cases:
Public test case 1:
Input:

Suba COA

Output:

Nandu: DL
Suba: COA

Public test case 2:


Input:

Pai CV

Output:

Nandu: DL
Pai: CV

Private test case 1:


Input:

Neha DS

Page 3
Output:
Nandu: DL
Neha: DS

Solution:

import java.util.*;
class Student{
String name;
String[] courses;
public Student(String n, String[] c) {
name = n;
courses=c;
}
public Student(Student s) {
this.name = s.name;
this.courses = new String[s.courses.length];
for(int i = 0; i < courses.length; i++) {
this.courses[i] = s.courses[i];
}
}
public void setName(String n) {
name = n;
}
public void setCourses(int indx, String c) {
courses[indx] = c;
}
public String getName() {
return name;
}
public String getCourses(int indx) {
return courses[indx];
}
}
public class Admission {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] courses = {"Maths", "DL","DSA","DC"};
Student s1 = new Student("Nandu", courses);
Student s2 = new Student(s1);
s2.setName(sc.next());
s2.setCourses(1,sc.next());

Page 4
System.out.println(s1.getName() + ": "+ s1.getCourses(1));
System.out.println(s2.getName() + ": " + s2.getCourses(1));
}
}

Page 5
Question:
Write a Java code to print the name of a passenger whose PNR number is given. The name
should be printed only for valid PNR numbers. The code should use the concepts of inner
classes in Java, and should have the following functionality.
Class PassengerList has/should have the following members:

• A list of valid PNR numbers pnrList as an instance variable.

• Constructor PassengerList() populates the list pnrList as given in the template


code.

• Method public PassengerInfo getPassengerInfo(String) should take a PNR num-


ber as argument, check if the PNR number is valid, and if yes, then return a valid
PassengerInfo object that has the name of the passenger. For ease of implementa-
tion, we assume that the valid PNR numbers are 1 to 3.

• An inner private class PassengerInfo

– Name of a specific passenger as an instance variable.


– This class implements interface IPassengerInfo, which enables its object to be
accessible from outside the class PassengerList.
– Constructor PassengerInfo(String) assigns the name of the passenger to the
instance variable of class PassengerInfo whose PNR number is given. If the
PNR number is i, then the name should be Passenger i, where the valid PNR
numbers are for i ranging from 1 to 3.

Class PassengerListTest has the main method.


• It accepts a PNR number as input, and invokes the necessary method to obtain the
passenger name for that PNR number.
Test Cases:
Public Test Cases:
Test Case 1:
Input:
1
Output:
Passenger 1
Test Case 2:
Input:
4
Output:
Not a valid passenger

Page 6
Private Test Cases:
Test Case 1:

Input:
2
Output:
Passenger 2

Test Case 2:

Input:
4123
Output:
Not a valid passenger

Solution:
import java.util.*;

interface IPassengerInfo {
public void printName();
}

class PassengerList {
private ArrayList<String> pnrList;

public PassengerList() {
pnrList = new ArrayList<String>();
for (int i = 1; i < 4; i++) {
pnrList.add("" + i);
}
}

public PassengerInfo getPassengerInfo(String passPnr) {


PassengerInfo pInfo = null;
if (pnrList.contains(passPnr))
pInfo = new PassengerInfo(passPnr);
return pInfo;
}

private class PassengerInfo implements IPassengerInfo {


String passName;

public PassengerInfo(String sPnr) {

Page 7
passName = "Passenger " + sPnr;
}

public void printName() {


System.out.println(passName);
}
}
}

public class PassengerListTest {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String passPnr = sc.nextLine();
PassengerList passList = new PassengerList();
IPassengerInfo passInfo = passList.getPassengerInfo(passPnr);
if (passInfo == null) {
System.out.println("Not a valid passenger");
} else {
passInfo.printName();
}
sc.close();
}
}

Page 8
Question:
Complete the Java program to demonstrate the use of abstract classes and interfaces. You
have to complete the definition of classes JuniorRS and SeniorRS to obtain the output as
given in the public test cases.

• Interface IResearchScholar has two methods: public void teaches(String str)


and public void studies(String str).

• Define classes JuniorRS and SeniorRS such that JuniorRS implements IResearchScholar
and SeniorRS extends JuniorRS.

• Class InterAbstrTest extends SeniorRS, and has the main method. An object of
JuniorRS invokes the method studies, and an object of SeniorRS invokes methods
studies and teaches.

Public Test Cases:


Test Case 1:
Input 1:
Python
Java
Output 1:
TA studies Python
TA studies Java
TA teaches Java
Test Case 2:
Input 2:
Cloud computing
Data Mining
Output 2:
TA studies Cloud computing
TA studies Data Mining
TA teaches Data Mining
Private Test Cases:
Test Case 1:
Input 1:
Machine Learning
Machine Learning
Output 1:
TA studies Machine Learning
TA studies Machine Learning
TA teaches Machine Learning

Page 9
Test Case 2:
Input 2:
Cloud computing
Data Mining
Output 2:
TA studies Cloud computing
TA studies Data Mining
TA teaches Data Mining

Solution:
import java.util.Scanner;

interface IResearchScholar {
public void teaches(String str);
public void studies(String str);
}

abstract class JuniorRS implements IResearchScholar {


public void studies(String str1) {
System.out.println("TA studies " + str1);
}
}

class SeniorRS extends JuniorRS {


public void teaches(String str) {
System.out.println("TA teaches " + str);
}
}

public class InterAbstrTest extends SeniorRS {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
JuniorRS jrs = new InterAbstrTest();
SeniorRS srs = new InterAbstrTest();
jrs.studies(str1);
srs.studies(str2);
srs.teaches(str2);
sc.close();
}
}

Page 10
Dynamic Dispatch
Problem Statement
Complete the Java code that uses the concept of inheritance to demonstrate dynamic method
dispatching.

• Create a class Vehicle with the following members:

– Private instance variable name.


– Constructor to initialize name.
– Accessor method for name.
– Method display to display the text: ”This is a generic vehicle.”

Classes Car and Bicycle should be defined in such a way that any object of Car or
Bycycle can be assigned to a reference variable of type Vehicle. See the main method
to understand the context.

– For Car, the method display should print: ”This is a car named name .”
– For Bicycle, the method display should print: ”This is a bicycle named name .”

In the main method of the DispatchExample class, create an array of Vehicle objects with
size 3.

• Initialize the first element with a generic vehicle (you can use an empty string for its
name).

• Initialize the second and third elements with a Car and a Bicycle, respectively, by
taking the vehicle’s name as input from the user.

Iterate over the array and call the display method for each vehicle.

Sample Input/Output
Input:

BMW
Giant

Output:

This is a generic vehicle.


This is a car named BMW.
This is a bicycle named Giant.

Page 11
Template Code
import java.util.Scanner;

class Vehicle {
private String name;

public Vehicle(String n) {
name = n;
}
// Define method display
// Define an accessor method

//Define class Car


//Define class Bicycle

public class DispatchExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Vehicle[] vehicles = new Vehicle[3];

vehicles[0] = new Vehicle("");


vehicles[1] = new Car(sc.nextLine());
vehicles[2] = new Bicycle(sc.nextLine());

for (Vehicle v : vehicles) {


v.display();
}

sc.close();
}
}

Solution
import java.util.Scanner;

class Vehicle {
private String name;

public Vehicle(String n) {
name = n;

Page 12
}

public String getName() {


return name;
}

public void display() {


System.out.println("This is a generic vehicle.");
}
}

class Car extends Vehicle {


public Car(String n) {
super(n);
}

public void display() {


System.out.println("This is a car named " + getName() + ".");
}
}

class Bicycle extends Vehicle {


public Bicycle(String n) {
super(n);
}

public void display() {


System.out.println("This is a bicycle named " + getName() + ".");
}
}

public class DispatchExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Vehicle[] vehicles = new Vehicle[3];

vehicles[0] = new Vehicle("");


vehicles[1] = new Car(sc.nextLine());
vehicles[2] = new Bicycle(sc.nextLine());

for (Vehicle v : vehicles) {


v.display();
}

Page 13
sc.close();
}
}

Page 14
1.2 Session 1 Type 4 Copy 1 Maps
Problem Statement
The Java program below takes as input the names of cricket players in a team and the runs
scored by each of them in 3 consecutive matches. The program is supposed to print the
names of those players who have scored at least 80 runs in all the matches. Complete the
code to obtain the specified output.
Class Team has the following members:

• Instance variable Map<String, ArrayList<Integer>> playerMap (maps the player


name to the list of runs scored by him/her in each match).

• A constructor to initialize the instance variable.

• An accessor method to access the instance variable.

Class FClass has the following members:

• A method getFinalList( ) that accepts an object of class Team as input and returns
the list of player names who has/have scored at least 80 runs in all the matches.

• main( ) method does the following:

– accepts inputs to instantiate an object of Team. The input is accepted in the


order - player name followed by the list of his/her runs.

– invokes method getFinalList( ) by passing an object of Team as input, to


return the list of player names who has/have scored at least 80 runs in all the
matches.

– prints the list returned by the method getFinalList( )

What you have to do


• Define method getFinalList( ) of class FClass

Test Cases
Public test case 1 (Input):

Ravi 76 76 76
sonu 80 80 89
viral 98 47 99

Output:

[sonu]

Page 15
Test Cases
Public test case 2 (Input):
P1 79 80 45
P2 88 46 90
P3 89 56 21
Output:
[]
Private test case 1 (Input):
P1 82 97 120
P2 80 90 99
P3 87 112 145
Output:
[P1, P2, P3]
Private test case 2 (Input):
P1 23 90 92
P2 88 65 78
P3 80 80 80
Output:
[P3]

Template Code
import java.util.*;

class Team{
private Map<String, ArrayList<Integer>> playerMap;
public Team( Map<String, ArrayList<Integer>> m) {
playerMap = m;
}
public Map<String, ArrayList<Integer>> getPlayerMap(){
return playerMap;
}
}

public class FClass{


public static ArrayList<String> getFinalList(Team t) {
// Define the method getFinalList( ) here

Page 16
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, ArrayList<Integer>> pmap =
new LinkedHashMap<String, ArrayList<Integer>>();

for(int i = 0; i < 3; i++) {


ArrayList<Integer> pruns = new ArrayList<Integer>();
String name = sc.next();
for(int j = 0; j < 3; j++) {
pruns.add(sc.nextInt());
}
pmap.put(name, pruns);
}

Team t = new Team(pmap);


System.out.println(getFinalList(t));
}
}

Solution:

import java.util.*;

class Team{
private Map<String, ArrayList<Integer>> playerMap;
public Team( Map<String, ArrayList<Integer>> m) {
playerMap = m;
}
public Map<String, ArrayList<Integer>> getPlayerMap(){
return playerMap;
}
}

public class FClass{


public static ArrayList<String> getFinalList(Team t) {
ArrayList<String> pList = new ArrayList<String>();
Map<String, ArrayList<Integer>> pmap = t.getPlayerMap();
for(String p : pmap.keySet()) {
boolean flag = true;
for(Integer i: pmap.get(p)) {

Page 17
if(i < 80) {
flag = false;
break;
}
}
if(flag)
pList.add(p);
}
return pList;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, ArrayList<Integer>> pmap =
new LinkedHashMap<String, ArrayList<Integer>>();

for(int i = 0; i < 3; i++) {


ArrayList<Integer> pruns = new ArrayList<Integer>();
String name = sc.next();
for(int j = 0; j < 3; j++) {
pruns.add(sc.nextInt());
}
pmap.put(name, pruns);
}

Team t = new Team(pmap);


System.out.println(getFinalList(t));
}
}

Page 18
Problem Statement
The publication information of a book has two parts: month and year of publication. Write
a Java program that accepts as inputs the year and month of a number of Publication
objects, stores the Publication objects in a TreeSet object, and prints the Publication
objects in sorted order.

• Class Publication implements Comparable<Publication> and has/should have the


following members:

– int year, int month as private instance variables

– Constructor to initialize the instance variables

– Method toString to print a Publication object in the format shown in the


sample input-output

– Define method compareTo to compare two Publication objects. It compares


between two publications as follows:

∗ If year of one publication is greater than another then that is the latest
publication.
∗ If years are same for both the publications then, compare the month. If
the month of one publication is greater than another then that is the latest
publication.
∗ Otherwise, both publications are equal.

• Class PublicationTest has the following members:

– main method accepts the year and month of 4 Publication objects and prints
the publication information in sorted order, removing the duplicates.

What you have to do


Define method compareTo in class Publication

Template Code

import java.util.*;
class Publication implements Comparable<Publication>{
private int year;
private int month;
public Publication(int y, int m){
year = y;
month = m;
}

Page 19
//****** DEFINE METHOD compareTo here
public String toString(){
return month + ", " + year;
}
}
class PublicationTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set pset = new TreeSet<Publication>();
for(int i = 0; i < 4; i++){
int y = sc.nextInt();
int m = sc.nextInt();
pset.add(new Publication(y, m));
}
for(var p : pset)
System.out.println(p);
}
}
Public test case 1:
Input:
2020 7
2022 9
2021 9
2021 1
Output:
7, 2020
1, 2021
9, 2021
9, 2022
Public test case 2:
Input:
2015 6
2023 7
2021 3
2021 3
Output:
6, 2015
3, 2021
7, 2023

Page 20
Private test case 1:
Input:
2017 7
1999 7
2015 9
2018 3
Output:
7, 1999
9, 2015
7, 2017
3, 2018
Private test case 2:
Input:
2011 1
2022 2
2020 3
2005 6
Output:
6, 2005
1, 2011
3, 2020
2, 2022

Solution:

import java.util.*;
class Publication implements Comparable<Publication>{
private int year;
private int month;
public Publication(int y, int m){
year = y;
month = m;
}
public int compareTo(Publication pub) {
if (this.year > pub.year) {
return 1;
}
else if(this.year < pub.year){

Page 21
return -1;
}
else if(this.month > pub.month){
return 1;
}
else if(this.month < pub.month){
return -1;
}
else{
return 0;
}
}
public String toString(){
return month + ", " + year;
}
}
class PublicationTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set pset = new TreeSet<Publication>();
for(int i = 0; i < 4; i++){
int y = sc.nextInt();
int m = sc.nextInt();
pset.add(new Publication(y, m));
}
for(var p : pset)
System.out.println(p);
}
}

Page 22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy