Midsem 2021 (Solved)

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

Name___________________ ID NUMBER_________________

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI


DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION SYSTEMS
OBJECT-ORIENTED PROGRAMMING (CSF212)
1ST SEMESTER 2021-22 MID-SEMESTER EXAMINATION
COMPLETELY CLOSED BOOK PART-A
MAX TIME: 60 Minutes MAX MARKS 30
------
Instructions:
1. Return this sheet within the first 60 minutes.
2. Answer the questions in this Sheet Itself. Answers at space provided. Answers at any other place
won’t be accepted.
3. Write the correct option for questions number 1,2, and 3 in the table below. Overwriting won’t be
accepted. Don’t ask the invigilator to sign after overwriting.
4. There is NO negative marking for any question.
5. Answer in the space provided for the rest of the questions.

Q1 Q2 Q3

Multiple Choice Questions:

1. Given the below code, what is the result? [3]


1. void go(){
2. int c = 0;
3. int []t = {1,2,3};
4. for(int i=0; i<4; i++)
5. switch(t[i]){
6. case 2: c++;
7. case 1: c+=10;
8. case 0: go();
9. }
10. System.out.println(c);
11. }
12. }

a) Compilation fails
b) 21
c) Stack Overflow Error
d) 28
e) 29

2. Given the below code, what is the result? [4]

1
1. public class Abc {
2. public static String s = "";
3. public static void compute() {
4. s +="Computing ";
5. }
6. }
7. public class Xyz extends Abc {
8. public static void compute() {
9. s += "Suspension";
10. }
11. public static void main(String args[]) {
12. Abc b1 = new Abc ();
13. Abc b2 = new Xyz();
14. Abc b3 = (Xyz)b2;
15. Xyz b4 = (Xyz)b2;
16. b1.compute();
17. b1.compute();
18. b3.compute();
19. b4.compute();
20. System.out.println(s);
21. }
22. }

a) Computing Suspension Computing


b) Computing Computing Suspension Suspension
c) Computing Computing Computing
d) Computing Computing Computing Suspension
e) Suspension Suspension Suspension
f) Suspension Suspension Suspension Computing
g) An exception is thrown at runtime

3. Given the below code, what is the result? [4]

1. abstract class Abc {


2. static String str = "";
3. Abc(){
4. str += "t ";
5. }
6. }
7. public class Xyz extends Abc {
8. Xyz(){
9. str += "s ";
10. }
11. Xyz(String str1){
12. str += str1;
13. }
14. }
15. public class Mno extends Xyz{

2
16. Mno(String str1){
17. str += str1;
18. new Xyz(str);
19. }
20. public static void main(String args[]) {
21. new Mno("tu ");
22. System.out.println(str);
23. }
24. }

a) s tu s tu s
b) t tu t s tu t t tu t s tu t s
c) t s tu t t s tu
d) t st tu tu tu s tu t s
e) Compilation fails.
f) Runtime exception

Writing correct code:


4. Write the signature for the generic child class Xyz whose parent is a generic
class Abc <T>. Write Exact Java Syntax. [4]

1. public class Abc <T> {


2. T obj1;
3. }
4. // WRITE SIGNATURE OF THE SUBCLASS Xyz
5. U obj2;
6. Xyz(T obj1, U obj2){
7. this.obj1 = obj1;
8. this.obj2 = obj2;
9. }
10. void show(){
11. System.out.println("T = "+obj1+ "U = "+obj2);
12. }
13. public static void main(String args[]) {
14. Xyz s = new Xyz(1.0, 2);
15. s.show();
16. }
17. }

ANS: ___________________________________________________

5. Which single word should be replaced at /* MISSING STATEMENT */ so that the


output is Java Programming. Write exact Java code. [4]
1. public class Abc{
2. private Abc(){
3. System.out.println("Java");
4. }
5. protected Abc(int a) {

3
6. /* MISSING CODE */
7. System.out.println("Programming");
8. }
9. }
10. public class Xyz extends Abc{
11. Xyz(int a){
12. super(a);
13. }
14. public static void main(String args[]) {
15. new Xyz(10);
16. }
17.
18. }
ANS: _________________________________________________________

___________________________________________________________________

Scenario-based question:

1. Refer to the below illustration of various entities in IPL. You are required to clearly
write classes and their association with each other. Also, figure out the methods
of each entity and their polymorphism (if exist). [11]

4
Blank page for the answer.

5
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI
DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION SYSTEMS
OBJECT-ORIENTED PROGRAMMING (CSF212)
1ST SEMESTER 2021-22 MID-SEMESTER EXAMINATION
COMPLETELY CLOSED BOOK
PART-B
MAX TIME: 90 Minutes MAX MARKS 60
------
Instructions:
1. Answer all the parts of a question together in the answer sheet. Answers at separate
places will not be accepted.
2. Please don’t write any information not relevant to the question.
3. Use your time judiciously.

1. One of your friends ask you about the checked and unchecked exceptions. If it's a type
of exception, how do we check it at the compile time? Why would you call them
exceptions, when they are checked at compile-time? Explain the concept of exceptions
to very beginner moving towards the deep technical details of the types and broad
categories. Illustrate the classification of exceptions with diagram(s) of checked and
unchecked exceptions and their sub-classes.
[11 Marks]
(~ 6 to 9 Minutes)

2. Why do we need to have keywords like RuntimeException or


ArrayIndexOutOfBoundsException when we can get the same results by using
keyword Exception in the catch block instead of the above two?
[4 Marks]
(~3 to 5 Minutes)

3. I have used a very dynamic list of over a hundred million elements with lots of additions
and deletions over the course of my application. I need to access the elements in a quick
time. I need to access them with the index number. I do a lot of searching in the array. In
fact, over 99% of my operations are that of searching the element to check if the element
is present or not in the array. Analyse the following collections on the basis of the above
requirements.
a. ArrayList
b. LinkedList
c. HashSet
d. Stack [15 Marks]
(~ 7 to 10 Minutes)

1
4. Multitasking has been a dream of every person and is present in every resume for the
last 20 years. We know of multithreading in Java. What's the difference between
multitasking and multithreading? In Java multithreading gives non-deterministic results.
Why? What are two ways/concepts/techniques using which I can move towards
determinism in Java using multithreading? Discuss them in brief.
[5+5+5 = 15 Marks]
(~7 to 10 Minutes)

5. You have done a lot with streams in your career as a programmer. A junior asks you,
why would anyone use PrintWriter class to print on screen when he/she can do the
same with FileOutputStream class? Why should I use any of the above if you can use
classes like Scanner and System.out? Explain to your junior the technical details of all
four with the differences and utilities while explaining the purpose of each one of them.
[5+5+5 = 15 Marks]
(~10 to 12 Minutes)

-------------------------END------------------------------

2
Part A Solutions

Scenario based question:

Answer:

Bidding is a class with Bidding date and Bidding venue as its data members. Methods
include Announcement of tender, Bid submission, Bid for more than one city, and
Governing council approval. [1 mark]

Auction is a class with Player 1 andTeam 1 as objects of Player and Team classes
respectively as its data members. Methods include Confirm player, Decline player, and
Notify player. [1 mark]

Player is a class with Player name, Base price, and Nationality as its data members.
Methods include Batting performance, Bowling performance, Fielding performance,
Leadership qualities, and Overall score. [1 mark]

Team is a class with Team name, Coach name, and Team owner as its data members.
Methods include Playing eleven structure, Bid submission, Confirm player, and
Governing council approval. [1 mark]

Match is a class with Team 1 and 2 as objects of Team class as its data members.
Methods include Date of match, venue of match, and Governing council approval.
[1 mark]

Tournament is a class with Player all, Team all, and Match all as objects of Player,
Team, and Match classes respectively as its data members. Methods include IPL
schedule, Playing eleven structure, and Overall score. [1 mark]

Polymorphism: [1 * 5 = 5 marks]

1. Bid submission method in Bidding class and Team class.


2. Governing council approval method in Bidding class, Team class, and Match
class.
3. Confirm player method in Auction class and Team class.
4. Overall score method in Player class and Tournament class.
5. Playing eleven structure method in Team class and Tournament class.
Multiple Choice Questions:

1. Given the below code, what is the result? [3]


1. void go(){
2. int c = 0;
3. int []t = {1,2,3};
4. for(inti=0; i<4; i++)
5. switch(t[i]){
6. case 2: c++;
7. case 1: c+=10;
8. case 0: go();
9. }
10. System.out.println(c);
11. }
12. }

a) Compilation fails
b) 21
c) Stack Overflow Error
d) 28
e) 29

2. Given the below code, what is the result? [4]


1. public class Abc {
2. public static String s = "";
3. public static void compute() {
4. s +="Computing ";
5. }
6. }
7. public class Xyz extends Abc{
8. public static void compute() {
9. s += "Suspension";
10. }
11. public static void main(String args[]) {
12. Abc b1 = new Abc();
13. Abc b2 = new Xyz();
14. Abc b3 = (Xyz)b2;
15. Xyz b4 = (Xyz)b2;
16. b1.compute();
17. b1.compute();
18. b3.compute();
19. b4.compute();
20. System.out.println(s);
21. }
22. }
a) Computing Suspension Computing
b) Computing Computing Suspension Suspension
c) Computing Computing Computing
d) Computing Computing Computing Suspension
e) Suspension Suspension Suspension
f) Suspension Suspension Suspension Computing
g) An exception is thrown at runtime

3. Given the below code, what is the result? [4]

1. abstract class Abc {


2. static String str = "";
3. Abc(){
4. str += "t ";
5. }
6. }
7. public class Xyz extends Abc{
8. Xyz(){
9. str += "s ";
10. }
11. Xyz(String str1){
12. str += str1;
13. }
14. }
15. public class Mno extends Xyz{
16. Mno(String str1){
17. str += str1;
18. new Xyz(str);
19. }
20. public static void main(String args[]) {
21. new Mno("tu ");
22. System.out.println(str);
23. }
24. }

a) s tu s tu s
b) t tu t s tu t ttu t s tu t s
c) t s tu t t s tu
d) t sttututu s tu t s
e) Compilation fails.
f) Runtime exception
Writing correct code:

4. Write the signature for the generic child class Xyz whose parent is a generic
class Abc<T>. Write Exact Java Syntax. [4]
1. public class Abc<T> {
2. T obj1;
3. }
4. // WRITE SIGNATURE OF THE SUBCLASS Xyz
5. U obj2;
6. Xyz(T obj1, U obj2){
7. this.obj1 = obj1;
8. this.obj2 = obj2;
9. }
10. void show(){
11. System.out.println("T = "+obj1+ "U = "+obj2);
12. }
13. public static void main(String args[]) {
14. Xyz s = new Xyz(1.0, 2);
15. s.show();
16. }
17. }
Answer: public class Xyz <T, U> extends Abc <T>

5. Whichsingle word should be replacedat /* MISSING STATEMENT */ so that the


output is Java Programming. Write exact Java code. [4]
1. public class Abc{
2. private Abc(){
3. System.out.println("Java");
4. }
5. protected Abc(int a) {
6. /* MISSING CODE */
7. System.out.println("Programming");
8. }
9. }
10. public class Xyz extends Abc{
11. Xyz(int a){
12. super(a);
13. }
14. public static void main(String args[]) {
15. new Xyz(10);
16. }
17.
18. }
Answer:this();
1. If some code within a method throws a checked excep on, then the method must either handle
the excep on or it must specify the excep on using the throws keyword. [2 marks]
They are called excep ons, because a user can make them unknowingly, and it should be
checked at compile me, so that the compiler won't move forward, unless the excep on is
handled]. [3 marks]
An Excep on is an error that on occurrence can terminate the program abnormally. [1 or 2
marks]
Refer book for categoriza on of excep ons [4 marks]
2. To specify the type of excep on, we think may occur {saves me of compiler}. Excep on
handlers that are too general can make code more error prone by catching and handling
excep ons that weren’t an cipated by the programmer and for which the handler was not
intended. [4 marks]
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti

You might also like