Sei sulla pagina 1di 18

Question Paper

Object Oriented Programming and Java (MC221) :January 2006


Section A : Basic Concepts (30 Marks)

1.

This section consists of questions with serial number 1 - 30.


Answer all questions.

Each question carries one mark.


Maximum time for answering Section A is 30 Minutes.

< Answer >

Mark the incorrect statement from the following:


(a)

Java is a fully object oriented language with strong support for proper software engineering
techniques
(b) In java it is not easy to write C-like so called procedural programs
(c) In java language objects have to be manipulated
(d) In java language error processing is built into the language
(e) Java is not a language for internet programming.
2.

(a) Copy of that object


(c) Memory address
(e) Default constructor.
3.

(b) Method called call by value


(d) Constructor

Which of the following is not a component of Java Integrated Development Environment (IDE)?
(a) Net Beans
(c) Symantecs Visual Caf
(e) Microsoft Visual J++.

4.

< Answer >

In java, objects are passed as

(b) Borlands Jbuilder


(d) Microsoft Visual Fox Pro
< Answer >

Identify, from among the following, the incorrect variable name(s).


(a) _theButton
(c) 2ndName
(e) my2ndFont.

< Answer >

(b) $reallyBigNumber
(d) CurrentWeatherStateofplanet

Use the following declaration and initialization to evaluate the Java expressions given in questions 5 - 8;
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
5.

(a) 21
6.

(d) 26

(e) 22.
< Answer >

(b) 2

(c) 4

(d) 10

(e) Synatax error.


< Answer >

(b) 3

(c) 2

(d) 1

(e) Syntax error.


< Answer >

System.out.println (c=c++);
(a) 2

9.

(c) 28

System.out.println (2U * ( g k ) +c);


(a) 6

8.

(b) 24

System.out.println(a++);
(a) 3

7.

< Answer >

System.out.println( - -b * a + c *d - -);

(b) 4

Consider the following Java program :

class IfStatement{

(c) 5

(d) 8

(e) Syntax error.


< Answer >

public static void main(String args[])


{
int a=2, b=3;
if (a==3)
if (b==3)
System.out.println("===============");
else
System.out.println("#################");
System.out.println("&&&&&&&&&&&");
}
}
Which of the following will the output be?
(a) ===============
(b) #################
&&&&&&&&&
(c) &&&&&&&&&&&
(d) ===============
#################
&&&&&&&&&&
(e) ################.
10. An applet cannot be viewed using
(a)
(b)
(c)
(d)
(e)

Netscape navigator
Microsoft Internet Explorer
Sun Hot Java Browser
Applet viewer tool which comes, with the Java Development Kit.
Jbuilder.

Use the following Java program for answering question 11 and 12


class test{
void meth(int i, int j)
{
i *= 2;
i /= 2;
}
}
class argumentPassing
{
public static void main(String args[])
{
test ob = new test();
int a = 15, b = 20;
System.out.println("a and b before call :"+ a +" " + b);
ob.meth(a,b);
System.out.println("a and b after call : "+ a + " " +b);
}
(Program III)
}

< Answer >

11.

< Answer >

What would the output be of the above Program - III before and after it is called?
(a)
(b)
(c)
(d)
(e)

and b before call : 15 20


a and b before call : 5 2
a and b before call : 15 20
a and b before call : 30 10
a and b before call : 15 20

a and b after call : 30 10


a and b after call : 15 20
a and b after call : 15 20
a and b after call : 15 20
a and b after call :
< Answer >

12. What would the argument passing method be which is used by the above Program - III?
(a) Call by value
(c) Call by java.lang class
(e) Call by compiler.

(b) Call by reference


(d) Call by byte code
< Answer >

13. Consider the following program:


class prob1{
int puzzel(int n){
int result;
if (n==1)
return 1;
result = puzzel(n-1) * n;
return result;
}
}
class prob2{
public static void main(String args[])
{
prob1 f = new prob1();
System.out.println(" puzzel of 6 is = " + f.puzzel(6));
}
}
Which of the following will be the output of the above program?
(a) 6

(b) 120

(c) 30

(d) 720

(e) 12.
< Answer >

14. The blank space in the following sentence has to be correctly filled :
Members of a class specified as .. are accessible only to methods of that class.
(a) Protected
(b) Final
(c) Public
(d) Private
(e) Static.

< Answer >

15. Java compiler javac translates Java source code into


(a) Assembler language
(c) Bit code
(e) Platform dependent code.

(b) Byte code


(d) Machine code
< Answer >

16. .. are used to document a program and improve its readability.


(a) System cells

(b) Keywords

(c) Comments

(d) Control structures (e) Blocks.

17. In Java, a character constants value is its integer value in the character set.
(a) EBCDIC

(b) Unicode

(c) ASCII

(d) Binary

(e) BCD.

< Answer >

18. In Java, a try block should immediately be followed by one or more .. blocks.
(a) Throw

(b) Run

(c) Exit

(d) Catch

(e) Error.

19. An abstract data type typically comprises a and a set of ... respectively.
(a) Data representation, classes
(c) Data representation, objects
(e) Data representation, operations.

(b) Polymorphism
(e) Overriding.

(b) Transferring

(c) Hiding

(d) Coupling

(b) Destructor

(c) Constructor

(d) Object

< Answer >

(e) extending.

22. In a class definition, the special method provided to be called to create an instance of that class is
known as a/an
(a) Interpreter

< Answer >

(c) Overloading

21. Re-implementing an inherited method in a sub class to perform a different task from the parent class is
called
(a) Binding

< Answer >

(b) Database, operations


(d) Control structure, operations

20. In object-oriented programming, the process by which one object acquires the properties of another
object is called
(a) Encapsulation
(d) Inheritance

< Answer >

< Answer >

(e) Compiler.

23. Consider the following statements about Java packages:

< Answer >

I.
Packages dont provide a mechanism to partition all class names into more manageable chunks.
II. Packages provide a visibility control mechanism.
III. One of the important properties of a package is that all classes defined inside a package is
accessible by code outside that package.
IV. The .class files for classes declared to be part of a package can be stored in multiple directories.
Which of them is correct?
(a) Only (I) above
(b) Only (II) above
(c) Only (III) above
(d) Only (IV) above
(e) All (I), (II), (III) and (IV) above are wrong.
< Answer >

24. Consider the following statements:


I.
II.
III.
IV.

A class can be declared as both abstract and final.


A class declared as final can be extended by defining a sub-class.
Resolving calls to methods dynamically at run-time is called late binding.
The class Object defined by Java need not be a super class of all other classes.

Identify the correct statement from the following:


(a) Both (I) and (II) above
(c) Both (I) and (III) above
(e) Only (III) above.

(b) Both (III) and (IV) above


(d) Both (II) and (IV) above

25. Identify, from among the following, the incorrect descriptions related to Java :

< Answer >

(a)

Java Virtual Machine translates byte code into its own systems machine language and runs the
resulting machine code
(b) The arithmetic operations *, /, %, + and have the same level of precedence
(c) Comments do not cause any action to be performed during the program execution
(d) All variables must be given a type when they are declared
(e) Java variable names are case-sensitive.
26. Consider the following statement(s) about Java:
I.
II.

All white-space characters (blanks) are ignored by the compiler.


Java keywords can be used as variable names.

< Answer >

III. An identifier does not begin with a digit and does not contain any spaces.
IV. The execution of Java applications begins at method main.
Which of them is correct?
(a) Both (I) and (III) above
(c) Both (I) and (II) above
(e) All (I), (II), (III) and (IV) above.

(b) Both (II) and (IV) above


(d) (III) and (IV) above
< Answer >

27. Consider the following data types in Java :


I. Int

II. Boolean

III. Double

IV. String

V. Array.

Which of them are simple data types?


(a) Both (I) and (II) above
(c) (I), (II) and (III) above
(e) All (I), (II), (III), (IV) and (V) above.

(b) (I), (II), (III) and (IV) above


(d) (II) and (III) above

28. For what values respectively of the variables gender and age would the Java expression gender == 1
&& age >= 65 become true?
(a) gender = 1, age = 60
(c) gender = 1, age = 65
(e) gender = 0, age = 55.

< Answer >

(b) gender = 1, age = 50


(d) gender = 0, age = 70
< Answer >

29. Consider the following Java program :


public class Compute {
public static void main (string args [ ])
{
int result, x ;
x=1;
result = 0;
while (x < = 10) {
if (x%2 = = 0) result + = x ;
++x;
}
System.out.println(result) ;
}
}
Which of the following will be the output of the above program?
(a) 55
(b) 30
(c) 25

(d) 35

(e) 45.

30. Which of the following statements about Java Threads is correct?


(a)
(b)
(c)
(d)
(e)

Java threads dont allow parts of a program to be executed in parallel


Java is a single-threaded language
Javas garbage collector runs as a high priority thread
Ready, running and sleeping are three states that a thread can be in during its life cycle
Every java application is not multithreaded.

END OF SECTION A

< Answer >

Section B : Problems (50 Marks)


This section consists of questions with serial number 1 5.
Answer all questions.
Marks are indicated against each question.
Detailed workings should form part of your answer.
Do not spend more than 110 - 120 minutes on Section B.
1.

Observe the following code and answer the following Questions:


1

interface Inter {

int number();

3 }
4 abstract class Abs {
5

static int foo = 12;

int number() { return 5; }

abstract int ace();

8 }
9 final class Sub extends Super {
10

Sub(int bar) { foo = bar; }

11

public int number() { return 10; }

12

int ace() { return 13; }

13

int dubble(int i) { return 2 * i; }

14 }
15 public class Super extends Abs implements Inter {
16

public int number() { return 11; }

17

public static void main(String args[]) {

18

Super s1 = new Super();

19

Super s2 = new Sub(16);

20

System.out.println(

);

21

22

int twice(int x) { return 2 * x; }

23

public int thrice(int x) { return 3 * x; }

24

int ace() { return 1; }

25

String dubble(String s) { return s + s; }

26 }
a.

In the above program, does Super correctly implement Inter? Why or why not?

b.

In line 11, it is a syntax error to omit the word public. Why?

c.

The method defined on line _____ overrides the method defined on line _____.

d.

The method defined on line _____ overloads the method defined on line _____.

e.

To print out foo with its original value of 12, you could put a print statement between lines _____ and _____.

f.

Which of the above classes and/or interfaces have default constructors?

g.

What effect does the keyword final on line 9 have?


(2 + 2 + 1 + 1 + 1 + 2 + 1 = 10 marks) < Answer >

2.

Write a program in java to find sin(x) value, sin(x) = x/1!x3/3!+ x5/5!___________


(10 marks) < Answer >

3.

4.

Write a java code to read 10 values and store on an array. Define your own exception name Wrong Mark
Exception. Throw and catch this type of exceptions when a value is <0 and >100.
(6 + 4 = 10 marks) < Answer >
Suppose we want to create a billing system for students at ICFAI University. The rates that the university charges
per credit hour are different for in-state and out-of-state students: $75 per credit hour for in-state students, $200 for
out-of-state students. Create the following three classes:
An abstract Student class containing fields for the two different rates, and a field for the students name. The class
should contain a display method to display the students name and total billing amount and a method to print out
the name of the university.
A class for an in-state student and one for an out-of-state student. Each class should contain a field for the number
of credit hours the student is taking, and a method to print out the universitys name, the students name, and the
total amount for the credit hours of the student.
Use the modifiers final and protected to ensure that a subclass can not change the rate the university charges or the
name of the university. Also, use abstract classes and methods when necessary and add the usual main method to
test your program.
(10 marks) < Answer >

5.

A company employs two types of worker. One, a salaried worker, gets paid a fixed weekly amount, while the
other, an hourly worker, is paid according to the number of hours worked. The system identifies each worker by an
identity number, and it also stores their surname. The company wishes to develop a payroll system which can be
used to compute the total weekly wages bill.
Develop a solution to this problem using inheritance and polymorphism. Your solution should contain:
a.

b.

Definitions of whatever classes are necessary. Provide a constructor, a method for computing the weekly
wage, and a to String method in each case. You do not have to provide set and get methods unless they are
required to support the payroll computation.
A test program which sets up an array of employees and prints out a line for each employee together with the
total weekly wages bill. Show what output you expect to obtain from your program.
(5 + 5 = 10 marks) < Answer >

END OF SECTION B

Section C : Applied Theory (20 Marks)


This section consists of questions with serial number 6.
Answer all questions.
Marks are indicated against each question.
Do not spend more than 25 -30 minutes on section C.

6.

a.
b.

(i) What are exceptions and errors in Java and how do they differ?
(ii) How does Javas exception mechanism help programmers to produce robust code?
(i) Name (but do not describe) five J Unit methods other than assert Equals, and specify what type of
arguments each method expects.
(ii) What are the four conditions for serializing an object?
(4 + 4 + 8 + 4 = 20 marks) < Answer >
END OF SECTION C
END OF QUESTION PAPER

Suggested Answers
Object Oriented Programming and Java (MC221) : January 2006
Section A : Basic Concepts
1.

Answer : (d)
Reason : Java is basically designed for compiler construction but
later on it is also being used for internet programming.

< TOP
>

2.

Answer : (c)
Reason : Objects are passed as memory address but not by the
constructor etc.,

< TOP
>

3.

Answer : (c)
Reason : Micro soft visual foxpro is not the part of IDE

< TOP
>

4.

Answer : (c)
Reason : A variable name should not start with numeric digit,

< TOP
>

5.

Answer : (b)
Reason : Based on the hierarchy of operations 24 is correct answer.

< TOP
>

6.

Answer : (b)
Reason : Based on the postincrementation operator 2 is the correct
answer.

< TOP
>

7.

Answer : (e)
Reason : Syntatically the expression is wrong.

< TOP
>

8.

Answer : (b)
Reason : first c is assigned by 2 and then incremented

< TOP
>

9.

Answer : (c)
Reason : C is the right choice because of the IF statement
hierarchy.

< TOP
>

10. Answer : (d)


Reason : Applet cant be viewed using JBuilder.

< TOP
>

11. Answer : (c)


Reason : There wont be any change because of the scope.

< TOP
>

12. Answer : (a)


Reason : Call by value is the right choice.

< TOP
>

13. Answer : (d)


Reason : 720 is the output after the execution of the program

< TOP
>

14. Answer : (d)


Reason : Private access specifier property says that members of
class of private are accessible only to the methods of that
class.

< TOP
>

15. Answer : (b)


Reason : Byte Code is the file which is generated after java source
file is compiled.

< TOP
>

16. Answer : (c)


Reason : Comments improve the readability of the program

< TOP
>

17. Answer : (b)


Reason : Java uses Unicode format.

< TOP
>

18. Answer : (d)


Reason : Try must be followed by catch block in exceptions in java.

< TOP
>

19. Answer : (e)


Reason : An abstract data type contains data representation and
operations

< TOP
>

20. Answer : (d)


Reason : Inheritance is the mechanism in which one object acquires
the properties of another object.

< TOP
>

21. Answer : (e)


Reason : Extending is the mechanism in which reimplementing an
inherited method in a sub class to perform a different task
from the parent class.

< TOP
>

22. Answer : (c)


Reason : According to the concept of Constructor C is right choice.

< TOP
>

23. Answer : (b)


Reason : Except b above all are contradictory to the functionality of
package.

< TOP
>

24. Answer : (e)


Reason : Except e above all are contradictory to the functionality of
classes concept of java

< TOP
>

25. Answer : (b)


Reason : The rule of precedence for arithmetic operations is *,/,
+or- from left to right.

< TOP
>

26. Answer : (d)


Reason : Remaining all are contradictory to the statements with
respect to java.

< TOP
>

27. Answer : (c)


Reason : String and array dont fall into the category of simple

< TOP
>

datatypes.
28. Answer : (c)
Reason : && is the short circuit operator which sees that all
conditions should be true then only it will evaluate to true.

< TOP
>

29. Answer : (b)


Reason : B is the right choice remaining all are contradictory to the
execution of program

< TOP
>

30. Answer : (d)


Reason : Ready, running and sleeping which are three states that a
thread can be in during its life cycle.

< TOP
>

Section B : Problems
1.

a.

Yes, because it defines number()

b.
c.
d.
e.

Because number() in Sub overrides number() in Super(), and you cannot make an overridden
function less public.
24 and 12, or 11 and 16, or 16 and 6 (any one pair)
13 and 25 (or 25 and 13)
17 and 19 (must be in a method, and must be done before an instance of Sub is created in line 19)

f.

Super (only)

g.

Prevents any other class from extending this one


< TOP >

2.

class sineser
{
float sum=0;
int pow(int x,int j)
{
for(int k=0;k<j;k++)
x=x*x;
return(x);
}
int fact(int x)
{
int f=1;
int fac=0;
if (x==1)
return(f);
for(int i=2;i<=x;i++)
{
f=f*i;
fac=fac+f;
}
return(fac);
}
void sinefn(int x,int n)
{
int j=1;

for(int i=2;i<=n+1;i++)
{
if(i%2==0)
{

sum=sum+(pow(x,j)/fact(j));
System.out.print("+"+x+"^"+j+"/"+j+"!");
}
else
{
sum=sum-(pow(x,j)/fact(j));
System.out.print("-" + x + "^" + j + "/" + j+ "!");
}
j=j+2;
}
System.out.println("Sum of Series :"+sum);
}
}
class series
{
public static void main(String args[])
{
int num,n;
num=Integer.parseInt(args[0]);
n=Integer.parseInt(args[1]);
sineser s=new sineser();
s.sinefn(num,n);
}
}
< TOP >

3.

Class WrongMarkException extends Exception


{
private int details;
WrongMarkException(int a)
{
details=a;
}
public String toString()
{
return("WrongMarkException[" +details +"]");
}
}
class exceptiondemo
{
static void compute(int val) throws WrongMarkException
{

if(val<0 || val>100)
throw new WrongMarkException(val);
}
public static void main(String args[])
{
int arr[]=new int[10];
for(int i;i<10;i++)
arr[i]=Integer.parseInt(args[i]);
try
{
for(int i=0;i<10;i++)
compute(arr[i]);

}catch (WrongMarkException e)
{
System.out.println("Caught"+e);
}
}
}
< TOP >

4.

We could choose the following approach:


To ensure that a subclass can not change the rates, we use the final modifier to declare the rate fields as constant in
the Student class.
To ensure that the universitys name does not change, we use a final method in the Student class.
Finally, since the student class can not actually print out any billing information for a student without knowing the
type of students and the number of credit hours, we declare the Student display method, and hence the entire class,
as abstract.
In addition, we can add the main method to this class instead of creating a separate class for testing everything,
which means, of course, that we will be able to directly run the class Student.
public abstract class Student
{ // fields
protected final double INSTATE_RATE = 75;
protected final double OUTSTATE_RATE = 200;
protected String studentName;
// methods
abstract void display();
void showSchoolName()
{
System.out.println("ICFAI University");
System.out.println("*********************");
}
public static void main(String args[])
{ // NOTE: The classes InStateStudent and OutStateStudents must be

// created before this class will compile correctly.


InStateStudent resident = new InStateStudent("Pinesh kumar", 24);
OutStateStudent alien = new OutStateStudent("Saurav Ganguly", 26);
resident.display();
alien.display();
}
}
class InStateStudent extends Student
{ // fields
private int creditHours;
// constructor
InStateStudent(String _studentName, int _creditHours)
{
studentName = _studentName;
creditHours = _creditHours;
}
void display()
{
showSchoolName();
System.out.println(studentName+" takes "+creditHours+" credits.");
System.out.println("In-state bill: " + creditHours * INSTATE_RATE);
}
}
class OutStateStudent extends Student
{ // fields
private int creditHours;
// constructor
OutStateStudent(String _studentName, int _creditHours)
{
studentName = _studentName;
creditHours = _creditHours;
}
void display()
{
showSchoolName();
System.out.println(studentName+" takes "+creditHours+" credits.");
System.out.println("Out--state bill: "+creditHours*OUTSTATE_RATE);
}
}
The last modifier we need to discuss is perhaps the most interesting one. It allows you to create rather tricky
classes, if desired, and also lets you easily define certain calculation methods, i.e. methods that only do some
calculations without having to manipulate any data other than their input parameters. Again, we will skip the
details, and focus only on static methods (leaving static fields alone).
< TOP >

5.

a.

One approach is to use an abstract class to represent workers, ensuring that it has an abstract method to

compute the wage. Subclasses for salaried workers and hourly paid workers then provide details specific to
these worker types. An alternative (less flexible) approach is to define a concrete class representing salaried
workers, then implement a subclass specialised for hourly workers (for an analogy, see the shopping Item
and ItemByWeightclasses in the lecture notes).
Note that an abstract class can still have a concrete constructor; it is called from the subclasses to initialise
the inherited instances variables for the ID and surname. The instance variables in the abstract class should be
protected.
public abstract class Worker {
protected int id;
protected String surname;
public Worker(int i, String s) {
id = i;
surname = s;
}
public abstract double weeklyWage();
}
The class for representing salaried workers:
public class SalariedWorker extends Worker {
private double weeklyWage;
public SalariedWorker(int i, String s, double w) {
super(i,s);
weeklyWage = w;
}
public double weeklyWage() {
return weeklyWage;
}
public String toString() {
return surname+ ID=+id+ (weekly wage = +weeklyWage+);
}
}
The class for representing workers paid by the hour:
public class HourlyWorker extends Worker {
private double hourlyRate;
private int hoursWorked;
public HourlyWorker(int i, String s, double r, int h) {
super(i,s);
hourlyRate = r;
hoursWorked = h;
}
public double weeklyWage() {
return hourlyRate*hoursWorked;
}
public String toString() {
return surname+ ID=+id+ (hourly rate = +hourlyRate+,
hours worked = +hoursWorked+);

b.

}
}
A suitable test program is shown below. The details are not too important so long as the array is declared to
be an array of Workerinstances, it is initialised with examples of both types of worker, and the total wage
bill is computed by calling the weeklyWagemethod for each array element.
public class ComputeWagesBill {
public static void main(String[ ] args) {
Worker[ ] staff = { new HourlyWorker(12,Ram,4.90,45),
new SalariedWorker(34,Ravi,250.0),
new HourlyWorker(56,Smita,5.50,60),
new SalariedWorker(78,Leela,320.0)};
double sum=0.0;
for (int i=0; i<staff.length; i++) {
System.out.println(staff[i]);
sum+=staff[i].weeklyWage();
}
System.out.println(Total weekly wage bill is +sum);
}
}
This produces the following output:
Ram ID=12 (hourly rate = 4.9, hours worked = 45)
Ravi ID=34 (weekly wage = 250.0)
Smita ID=56 (hourly rate = 5.5, hours worked = 60)
Leela ID=78 (weekly wage = 320.0)
Total weekly wage bill is 1120.5
< TOP >

Section C: Applied Theory


6.

a.

b.

(i)

An exception is an unusual situation from which it may be possible to recover, e.g.attempting to read
data from a file that cannot be located. An error is an unrecoverablecondition (e.g. running out of
memory).
(ii) The exception handling mechanism makes code more robust by separating the code for normal running
of the program from code for handling exceptional conditions. Other approaches (e.g., using multiple
conditional statements to check for errors) mix the normal and exceptional code together, and tend to
cause big increases in program size.
(i) assertFalse(boolean), assertNull(Object), assertNotNull(Object), assertSame(Object,Object),
assertNotSame(Object,Object), assertTrue(boolean), fail()
All of the above may also have a String as a first parameter.
(private) failNotEquals(String,Object,Object), failSame(String), failNotSame(String,Object,Object),
format(String,Object,Object)
(ii) i.
The class must be declared as public
ii. The class must implement Serializable
iii. The class must have a no-argument constructor
iv. All fields of the class must be serializable (or transient
< TOP >

< TOP OF THE DOCUMENT >

Potrebbero piacerti anche