Você está na página 1de 4

COMSATS - Lancaster Dual Degree Program

COMSATS Institute of Information Technology Lahore


S1 Examination Semester Spring 2013
Course Title:
Object Oriented Concepts and Programing Course Code:
Course Instructor/s: Muhammad Shahid Bhatti
Program Name:
nd
Semester:
2
Batch:
Section:

Time Allowed:

01 Hours

CSC245

Credit Hours: 4(3,1)

Date

Maximum Marks:

Students Name:

30

Reg. No.

Important Instructions / Guidelines:


All Questions are mandatory
Write all of answers on answer sheet
Consider java language for all answers
1. Write Short answers to the following questions:
[Marks 10]
1.1. What is the difference between class and objects?
[Marks 2]
1.2. What is purpose of new keyword in java?
[Marks 2]
1.3. Name at least five data types available in java.
2]
1.4. What is the difference between instance variable and class variable?
[Marks 2]
1.5. What will be the return type of x in the following java statement?
[Marks 2]

[Marks

___?_____ x=JOptionPane.showInputDialog(Enter a value :);


2. Predict the output of following Java Programs? [Marks 3*2]
2.1.

2.2.

class Test {
public static void main(String[] args) {
for(int i = 0; true; i+=2) {
System.out.println("Welcome to OOCP");
break;
}
}
}

publicclassQTest{
publicStringx,y;
x=5;
y=6;
}
classQ{
publicstaticvoidmain(Stringargs[]){
Testt=newTest();
System.out.println(t.x+t.y);
}
}
3. Correct the errors
3.1. Indentify the errors in the following code segments

intg(){
System.out.println("Insidemethodh");
inth(){
System.out.println("InsideMethodh");}
}

[Marks 4]

4.
[Marks 10]
4.1. Create a class called Employee that includes three pieces of information as instance
variables a first name (type String), a last name (type String) and a monthly salary
(double).
[Marks 02]
4.2. Your class should have a constructor that initializes the three instance variables.
[Marks 02]
4.3. Provide a set and a get method for each instance variable. If the monthly salary is not
positive, set it to 0.0.
[Marks 02]
4.4. Write a test application named EmployeeTest that demonstrates class Employees
capabilities. [Marks 02]
4.5. Create two Employee objects and display each objects yearly salary. Then give each
Employee a 10% raise and display each Employees yearly salary again.
[Marks 02]

Solution
1. Q. 1.
1.1. Object - Objects have states and behaviors. Example: A dog has states-color, name,
breed as well as behaviors -wagging, barking, eating. An object is an instance of a
class.
Class - A class can be defined as a template/ blue print that describe the
behaviors/states that object of its type support.
1.2. What is purpose of new keyword in java? [Marks 02]
Ans: The keyword new is used to instantiate or create Java class objects.
1.3. What are primitive data types?
[Marks 02]
Ans : byte, short, int, long float, double, boolean, char
1.4. What is the difference between instance variable and class variable? [Marks 02]
Answer: Instance variables have separate values for each instance of a class. Class
variables maintain a single shared value for all instances of the class, even if no
instance object of that class exists.
1.1. Answer: String
2. Predict the output of the following programs[Marks 3*2]
2.1. Welcome to OOCP
2.2. 56
3. Error Correction [Marks 04]
intg(){
System.out.println("Insidemethodh");
}
inth(){
System.out.println("InsideMethodh");
}
4. Solution
publicclassEmployeeTest{
publicstaticvoidmain(String[]args){
Employeeobj1=newEmployee("Abid","Ali",5000);
Employeeobj2=newEmployee("Muhammad","Akbar",6000);
System.out.println("AnnualSalaryofobject1
"+obj1.getSalary()*12);
System.out.println("AnnualSalaryofobject2
"+obj2.getSalary()*12);
obj1.setSalary(obj1.getSalary()*0.01+obj1.getSalary());
obj1.setSalary(obj2.getSalary()*0.01+obj2.getSalary());
System.out.println("Salaryafter10%increase");
System.out.println("AnnualSalaryofobject1
"+obj1.getSalary()*12);
System.out.println("AnnualSalaryofobject2
"+obj2.getSalary()*12);

}
}
classEmployee{
privateStringfirstName;
privateStringlastName;
privatedoublesalary;
Employee(StringfirstName,StringlastName,doublesalary){
this.firstName=firstName;
this.lastName=lastName;
this.salary=salary;
}
publicvoidsetFirstName(){
}
publicStringgetFirstName(){
returnfirstName;
}
publicvoidsetlastName(){

this
.

lastName
=
lastName;
}
publicStringgetLastName(){
returnlastName;
}
publicvoidsetSalary(doublesalary){
if(salary>0.0)
this.salary=salary;
else
this.salary=0.0;
}
publicdoublegetSalary(){
returnsalary;
}
}