OPPE REvision 1
OPPE REvision 1
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.
– 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.
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
Pai CV
Output:
Nandu: DL
Pai: CV
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:
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);
}
}
Page 7
passName = "Passenger " + sPnr;
}
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.
• 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.
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);
}
Page 10
Dynamic Dispatch
Problem Statement
Complete the Java code that uses the concept of inheritance to demonstrate dynamic method
dispatching.
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:
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
sc.close();
}
}
Solution
import java.util.Scanner;
class Vehicle {
private String name;
public Vehicle(String n) {
name = n;
Page 12
}
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:
• 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.
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;
}
}
Page 16
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, ArrayList<Integer>> pmap =
new LinkedHashMap<String, ArrayList<Integer>>();
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;
}
}
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>>();
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.
∗ 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.
– main method accepts the year and month of 4 Publication objects and prints
the publication information in sorted order, removing the duplicates.
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