Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH

Faculty of Science and Technology


Department of Computer Science
CSC 1205 Object Oriented Programming 1 (Section: ALL)
Mid Term Examination Spring 2019-20
Total Marks: 100 Moderator: Sifat Rahman Ahona Time: 2 hours
Specific Instructions:
1. There are [3] Parts [Part-A, Part-B and Part-C]. Answer all the Parts.
2. Answer Part-A in OMR sheet. Answer Part-B and Part-C in the Question Paper within the given space.
3. If required, you may Take signed loose sheet from the invigilator for answering Part-C.
General Instructions:
1. Return the question paper, and the OMR sheet at the end of the examination.
2. Use Pencil/Pen to write the answer and to draw diagrams. Use Pencil to fill the OMR sheet.
3. Marks on the right margin indicate full marks.
Name: Date:
ID: Permit No: Section: Invigilator Sign
Part – A: MCQ [Question No: 1-20]
[5 x 1 Mark per Question = 5]
Answer All
[15 x 2 Marks per Question = 30]
Use the following two options for questions 1 to 5.
a) True b) False
1. In java, String is not a primitive data type. 1

2. A method in a class can be static. 1

3. Type casting from a double variable to an int variable is an example of implicit type casting. 1

4. Keyword ‘this’ can be used as a reference and also for constructor chaining. 1

5. In java, a class can inherit more than one class. 1

6. Which of the following statement is invalid for array declaration? 2


a) int a[ ] = {1,2,3}; b) int a[3] = {1,2,3};
c) int [ ]a = new int [3]; d) int a[ ] = new int [ ]{1,2,3};
7. What is the correct statement about the following Java code snippet? 2
int ar[ ]={1,2,3,4,5,6,7};
System.out.println(ar[ar.length-1]);
b) The above program outputs an error message
a) The above program outputs a garbage value
and halts the execution
c) It will produce 7 d) It will produce 6
8. What will be output of the following Java code snippet? 2
Int [ ]arr = new int [5];
System.out.println(arr.length);
a) Error b) 0 c) 1 d) 5
9. Which of the following statement is correct for returning an array arr[ ] from a method? 2
a) return arr; b) return arr[ ]; c) return [ ]arr; d) return array;
10. What will be the output of the following Java code snippet? 2
String str1 = "OOP1";
String str2 = new String("OOP1");
System.out.print(str1.equals(str2));
System.out.print(str1 == str2);
a) truetrue b) truefalse c) falsetrue d) falsefalse
11. What will be the output of the following Java code snippet? 2

Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Page 1 of 6


String s1 = “Mid”;
String s2 = “Term”;
s1.concat(s2);
System.out.println(s1);
a) MidTerm b) Mid Term c) Mid d) Term
12. What will be output of the following Java code snippet? 2
String s = “”;
System.out.print(s.length( ));
a) null b) 0 c) 2 d) Error
13. What will be the output of the following Java code snippet? 2
String str1 = "Java";
Str1.toUpperCase( );
System.out.print(str1);
a) java b) JAVA c) Java d) Error
Use the following erroneous code for Question 14-17.
1. import java.io.*;
2. class Rectangle{
3. float length, width;
4. private Rectangle( ){ }
5. public Rectangle(float L, float W)
6. length=L:
7. width=W;
8. }
9. public void main(string [ ]error){
10. Rectangle r1 = Rectangle( );
11. Rectangle r2 = new Rectangle(1.0,2.0);
12. system.out.print(r1.L+" "+r2.W);
13. }
14. }
14. How many errors are there in the above Java code? 2
a) 8 b) 10 c) 12 d) 14
15. What is the error at line no. 1? 2
a) The keyword import is invalid b) The package io is invalid
Both the keyword import and package io is
c) d) No Error
invalid.
16. What will be the correct statement for line no. 6? 2
a) L=length; b) length=L,
c) length=L; d) No Error, the statement is correct.
17. How many errors are there in line no. 12? 2
a) 3 b) 4 c) 1 d) 2
18. The keyword _________________________ is used to denote a class variable. 2
a) new b) package c) class d) static
19. The keyword ‘new’ is responsible for ____________ 2
a) Allocating the memory b) Initializing Values
c) Declaring attributes d) All of the above
20. Making the variables private in a class refers to which concept of Object-Oriented Programming? 2
a) Inheritance b) Polymorphism c) Encapsulation d) Abstraction

Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Page 2 of 6


Part – B: Analytical Questions [21-22]
[2 x 20 = 40 Marks]
Answer All Question No 21 will be evaluated for OBE CO1
Name: Id: Sec:
CO1 Identify the principles used and trace the output of a java program.
Identification [5] Number of Statements [5] Program Logic Understanding [5] Correctness [5]

21. Trace the output of the following code program: 20


class Exam{
double marks;
int min;
public Exam( ){System.out.println("E-E");}
public Exam(int min, double marks){
this( );
this.min = min; this.marks = marks;
System.out.println("P-E");
}
public void show(String s){
System.out.println(s + "Exam");
System.out.println("Marks: "+marks+" Min: "+min);
}
}
class Midterm extends Exam{
private int day, slot;
public void setDay(int day){this.day = day;}
public void setSlot(int slot){this.slot = slot;}
public int getDay( ){return day;}
public int getSlot( ){return slot;}
public Midterm( ){super(120,100.0);}
public Midterm(int day, int slot, int min, double marks){
this.day = day; this.slot = slot;
super.min = min; super.marks = marks;
System.out.println("P-M");
}
public void show(String s){
super.show(s);
System.out.println("Day: "+slot+" Slot: "+day);
}
}
public class Output{
public static void main(String [ ]args){
Midterm setA = new Midterm(5, 3, 120, 100.0);
Midterm setB = new Midterm( );
setA.show("Mid");
setB.show("No");
}
}
Now, Identify the principles used in above code and justify your answer.

Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Page 3 of 6


22. Trace the output of the following program and answer the following question: 10+10
class Account{
private int accountNo;
Output
private double balance;
public static int perDayTransactionLimit; AccNo: 1111
public Account(int a, double b){ Balance: 2000.0
accountNo = a; balance = b; Limit: 5
perDayTransactionLimit = 5; AccNo: 2222
} Balance: 2500.0
public void printValues( ){ Limit: 7
System.out.println("AccNo: "+ accountNo); Confusing???
System.out.println("Balance: "+ balance); AccNo: 2222
System.out.println("Limit: "+ perDayTransactionLimit); Balance: 2500.0
} Limit: 8
public void changeValue(int n){
perDayTransactionLimit++;
System.out.println("Confusing???");
}
}
public class MainDemo{
public static void main(String args[ ]){
Account a1 = new Account(1111, 2000.0);
Account a2 = new Account(2222, 2500.0);
a1.printValues( );
Account.perDayTransactionLimit = 7;
a2.printValues( );
a1.changeValue(10);
a2.printValues( );
}
}

Now, Identify the followings from the above code:

a, b, n, args[ ]
Local Variables:

perDayTransactionLimit
Class Variables:

accountNo, balance
Instance Variables:

public Account(int a, double b)


Constructor:

a1, a2
Objects:

Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Page 4 of 6


Part – C: Program Writing [23-24]
[1 x 10 Marks = 10]
Answer All
[1 x 15 Marks = 15]
23. Look at the following Car class. Write only the lines of codes that required in the main( ) method of 10
MainClass, so that the program can provide output exactly like the Sample Output:

public class Car{


public Car( ){ System.out.println("Mazda Car"); } Sample Output:
public void carObj( ){ Car c = new Car( ); }
public static void displayColor( ){ Red Color
System.out.println("Red Color"); Mazda Car
} Mazda Car
}
public class MainClass{
public static void main(String s[ ]){
/* ---Write the Body of the main Method in the space below---*/
Car.displayColor( ); Car.displayColor( ); System.out.println(“Red Color”);
Car c1 = new Car( ); Car c1 = new Car( ); System.out.println(“Mazda Car”);
Car c2 = new Car( ); C1.carObj( ); System.out.println(“Mazda Car”);

}
}

24. Look at the following class descriptions and write the classes: 15

Person Doctor
String name String workingid
int age String specialization
Doctor( )
Person( )
Doctor(String name, int age, String workingid, String
Person(String name, int age)
specialization)
void setName(String name)
void setWorkingId(String workingid)
void setAge(int age)
void setSpecialization(String specialization)
String getName( )
String getWorkingId( )
int getAge( )
String getSpecialization( )
void showInfo( )
Special Note:

 The Doctor class inherits Person class.


 The showInfo( ) of Doctor class displays the details of a doctor including his name and age.
 main( ) method and a Sample Output for the program is as below:

public class MainActivity{


public static void main(String[ ] args){
Doctor d1=new Doctor("Devi Shetty", 45,"1101-1","Executive Director");
d1.showInfo( );
} Sample Output:
}
Name: Devi Shetty
Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Age: 45 Page 5 of 6
Working id: 1101-1
Designation: Executive Director
/*---- Best of Luck J ---- */

Output Encapsulation and Output


Inheritance was
E-E used. AccNo: 1111
P-M Balance: 2000.0
E-E Limit: 5
P-E AccNo: 2222
MidExam Balance: 2500.0
Marks: 100.0 Min: 120 Limit: 7
Day: 3 Slot: 5 Confusing???
NoExam AccNo: 2222
Marks: 100.0 Min: 120 Balance: 2500.0
Day: 0 Slot: 0 Limit: 8

Local Variables: a, b, n, args[ ]


Class Variables: perDayTransactionLimit
Instance Variables: accountNo, balance
Constructor: public Account(int a, double b)
Objects: a1, a2

Car.displayColor( ); Car.displayColor( ); System.out.println(“Red Color”);


Car c1 = new Car( ); Car c1 = new Car( ); System.out.println(“Mazda Car”);
Car c2 = new Car( ); C1.carObj( ); System.out.println(“Mazda Car”);

Person Doctor
String name String workingid
int age String specialization
Doctor( )
Person( )
Doctor(String name, int age, String workingid, String
Person(String name, int age)
specialization)
void setName(String name)
void setWorkingId(String workingid)
void setAge(int age)
void setSpecialization(String specialization)
String getName( )
String getWorkingId( )
int getAge( )
String getSpecialization( )
void showInfo( )
Special Note:

 The Doctor class inherits Person class.


 The showInfo( ) of Doctor class displays the details of a doctor including his name and age.
 main( ) method and a Sample Output for the program is as below:

Spring 19-20 CSC 1205 Object Oriented Programming 1 (JAVA) Page 6 of 6

You might also like