object oriented programming and java (mc221) january 2006.doc

Upload: amitukumar

Post on 10-Jan-2016

31 views

Category:

Documents


2 download

TRANSCRIPT

Question PaperObject Oriented Programming and Java (MC221) :January 2006

Section A : Basic Concepts (30 Marks)

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.

1.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.< Answer >

2.In java, objects are passed as

(a) Copy of that object(b) Method called call by value

(c) Memory address

(d) Constructor

(e) Default constructor.< Answer >

3.Which of the following is not a component of Java Integrated Development Environment (IDE)?

(a) Net Beans

(b) Borlands Jbuilder(c) Symantecs Visual Caf(d) Microsoft Visual Fox Pro(e) Microsoft Visual J++.< Answer >

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

(b) $reallyBigNumber(c) 2ndName

(d) CurrentWeatherStateofplanet(e) my2ndFont.< Answer >

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.System.out.println( - -b * a + c *d - -);

(a) 21(b) 24(c) 28(d) 26(e) 22.< Answer >

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

(a) 3(b) 2(c) 4(d) 10 (e) Synatax error.< Answer >

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

(a) 6(b) 3(c) 2(d) 1 (e) Syntax error.< Answer >

8.System.out.println (c=c++);

(a) 2(b) 4(c) 5(d) 8 (e) Syntax error.< Answer >

9.Consider the following Java program :

class IfStatement{

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)################.< Answer >

10.An applet cannot be viewed using

(a)Netscape navigator

(b)Microsoft Internet Explorer(c)Sun Hot Java Browser(d)Applet viewer tool which comes, with the Java Development Kit.(e)Jbuilder.< Answer >

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);

}

}

11.What would the output be of the above Program - III before and after it is called?

(a)and b before call : 15 20

a and b after call : 30 10

(b)a and b before call : 5 2

a and b after call : 15 20

(c)a and b before call : 15 20a and b after call : 15 20

(d)a and b before call : 30 10a and b after call : 15 20

(e)a and b before call : 15 20a 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

(b) Call by reference

(c) Call by java.lang class(d) Call by byte code

(e) Call by compiler.< 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(b) Byte code

(c) Bit code

(d) Machine code

(e) Platform dependent 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.< Answer >

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.< Answer >

19.An abstract data type typically comprises a and a set of ... respectively.

(a) Data representation, classes(b) Database, operations

(c) Data representation, objects(d) Control structure, operations

(e) Data representation, operations.< Answer >

20.In object-oriented programming, the process by which one object acquires the properties of another object is called

(a) Encapsulation

(b) Polymorphism(c) Overloading

(d) Inheritance

(e) Overriding.< Answer >

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

(a) Binding(b) Transferring(c) Hiding(d) Coupling (e) extending.< Answer >

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(b) Destructor(c) Constructor(d) Object (e) Compiler.< Answer >

23.Consider the following statements about Java packages:

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.A class can be declared as both abstract and final.

II.A class declared as final can be extended by defining a sub-class.

III.Resolving calls to methods dynamically at run-time is called late binding.

IV.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(b) Both (III) and (IV) above

(c) Both (I) and (III) above(d) Both (II) and (IV) above

(e) Only (III) above.< Answer >

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

(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.< Answer >

26.Consider the following statement(s) about Java:

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

II.Java keywords can be used as variable names.

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(b) Both (II) and (IV) above

(c) Both (I) and (II) above(d) (III) and (IV) above(e) All (I), (II), (III) and (IV) above.< Answer >

27.Consider the following data types in Java :

I. IntII. BooleanIII. DoubleIV. StringV. Array.

Which of them are simple data types?

(a) Both (I) and (II) above(b) (I), (II), (III) and (IV) above(c) (I), (II) and (III) above(d) (II) and (III) above(e) All (I), (II), (III), (IV) and (V) above.< Answer >

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(b) gender = 1, age = 50(c) gender = 1, age = 65(d) gender = 0, age = 70(e) gender = 0, age = 55.< 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.< Answer >

30.Which of the following statements about Java Threads is correct?(a)Java threads dont allow parts of a program to be executed in parallel(b)Java is a single-threaded language(c)Javas garbage collector runs as a high priority thread(d)Ready, running and sleeping are three states that a thread can be in during its life cycle(e)Every java application is not multithreaded.< Answer >

END OF SECTION A

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:

1interface Inter {

2 int number();

3 }

4 abstract class Abs {

5 static int foo = 12;

6 int number() { return 5; }

7 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. 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 100.

(6 + 4 = 10 marks) < Answer >4.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.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.

b. 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.(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?

b.(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 AnswersObject Oriented Programming and Java (MC221) : January 2006

Section A : Basic Concepts1.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 datatypes.< TOP >

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.Because number() in Sub overrides number() in Super(), and you cannot make an overridden function less public.

c.24 and 12, or 11 and 16, or 16 and 6 (any one pair)

d.13 and 25 (or 25 and 13)

e.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

(Program III)