object oriented programming and java (mc221) july 2007.doc

Upload: amitukumar

Post on 10-Jan-2016

11 views

Category:

Documents


0 download

TRANSCRIPT

Suggested Answers with Examiner's Feedback

Question PaperObject Oriented Programming and Java (MC221) : July 2007

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.Which of the following can Java run from a Web browser exclusively?

(a)

Applications

(b)

Applets

(c)

Servlets

(d)

Micro Edition programs

(e)

All of the above.

< Answer >

2.Which of the following language is Architecture-Neutral?

(a)

Java

(b)

C++

(c)

C

(d)

Ada

(e)

Pascal.

< Answer >

3.How the main method header is written in Java?

(a)

public static void main(string[] args)

(b)

public static void Main(String[] args)

(c)

public static void main(String[] args)

(d)

public static main(String[] args)

(e)

public void main(String[] args).

< Answer >

4.What is the extension name of a Java source code file?

(a)

.java(b)

.obj(c)

.class(d)

.exe

(e)

.javac.

< Answer >

5.Which of the following is not the reserved words in java? (a)

Public

(b)

Static

(c)

Void

(d)

Class

(e)

Num.

< Answer >

6.Suppose

static void nPrint(String message, int n) {

while (n > 0) {

System.out.print(message);

n--;

}

}

What is the printout of the call Print('a', 4)?

(a)

aaaaa(b)

aaaa(c)

aaa(d)

aa

(e)

invalid call.

< Answer >

7.Analyze the following code.

public class Test {

public static void main(String[] args) {

System.out.println(max(1, 2));

}

public static double max(int num1, double num2) {

System.out.println("max(int, double) is invoked");

if (num1 > num2)

return num1;

else

return num2;

}

public static double max(double num1, int num2) {

System.out.println("max(double, int) is invoked");

if (num1 > num2)

return num1;

else

return num2;

}

}

(a)

The program cannot compile because you cannot have the print statement in a non-void method

(b)

The program cannot compile because the compiler cannot determine which max method should be invoked

(c)

The program runs and prints 2 followed by "max(int, double)" is invoked

(d)

The program runs and prints 2 followed by "max(double, int)" is invoked

(e)

The program runs and prints "max(int, double) is invoked" followed by 2.

< Answer >

8.Analyze the following code.

public class Test {

public static void main(String[] args) {

System.out.println(m(2));

}

public static int m(int num) {

return num;

}

public static void m(int num) {

System.out.println(num);

}

}

(a)

The program has a syntax error because the two methods m have the same signature

(b)

The program has a syntax error because the second m method is defined, but not invoked in the main method

(c)

The program runs and prints 2 once

(d)

The program runs and prints 2 twice

(e)

The program runs and prints 2 thrice.

< Answer >

9.What is Math.rint(3.5)?

(a)

3.0

(b)

3

(c)

4

(d)

4.0

(e)

5.0.

< Answer >

10.Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = new int[5];

int i;

for (i = 0; i < x.length; i++)

x[i] = i;

System.out.println(x[i]);

}

}

(a)

The program displays 0 1 2 3 4

(b)

The program displays 4

(c)

The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException

(d)

The program has syntax error because i is not defined in the last statement in the main method

(e)

The program displays 1 2 3 4 5.

< Answer >

11.Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] oldList = {1, 2, 3, 4, 5};

reverse(oldList);

for (int i = 0; i < oldList.length; i++)

System.out.print(oldList[i] + " ");

}

public static void reverse(int[] list) {

int[] newList = new int[list.length];

for (int i = 0; i < list.length; i++)

newList[i] = list[list.length - 1 - i];

list = newList;

}

}

(a)

The program displays 1 2 3 4 5

(b)

The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException

(c)

The program displays 5 4 3 2 1

(d)

The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException

(e)

The program displays 54321 and doesnot raise an arrayIndexOutOfBoundsException.

< Answer >

12.If n were declared to be an int and were assigned a non-negative value, the statement:

if (n / 10 % 10 == 3) System.out.println("Bingo!");

will display the message if and only if:

(a)

n is divisible (divides evenly) by 3

(b)

n is divisible (divides evenly) by 30

(c)

The units' digit (also known as the 1's place) of n is 3

(d)

The tens' digit of n is 3

(e)

The remainder when n is dividied by 30 equals 3.

< Answer >

13.The Java program:

public class Practice2 {

public static void main(String args[]) {

for(int row = -2; row

15.The Java expression:

! ((b != 0) || (c 5))

(b)

(b == 0) && (c > 5)

(c)

(b != 0) && (c

18.You may or may not recall that the standard Java computing environment contains a Math class that includes a method called random() which returns a double as described below:

public class Math {

// The following method returns a double value with a

// positive sign, greater than or equal to zero,

// but less then 1.0, chosen "randomly" with approximately

// uniform distribution from that range.

public static double random() { ... }

...

}

Which one of the following expressions has a value that is equally likely to have any integer value in the range 3..8?

(a)

(6 * (int)Math.random()*100) + 3

(b)

(Math.random()*100 / (int) 6) + 3

(c)

(3..8 * Math.random()*100)

(d)

(int)(Math.random()*100 % 6) + 3

(e)

(6 / (int) Math.random()*100) + 3.

< Answer >

19.Which of the following statement is false?

(a)

The general approach of writing programs that are easy to modify is the central theme behind the design methodology called software engineering

(b)

Methods in the same class that have the same name but different signatures are called overloaded methods

(c)

An instance of a class is an object in object-oriented programming

(d)

When methods are executed, they are loaded onto the program stack in RAM

(e)

To access a method of an object we write ..

< Answer >

20.Consider the following statements:

double mint[ ];

mint = new double[10];

Which of the following is an incorrect usage of System.in.read() with this array?

(a)

mint[0] = (double) System.in.read();

(b)

mint[1%1+1] = System.in.read();

(c)

mint[2] = (char) System.in.read();

(d)

mint[3] = System.in.read() * 2.5;

(e)

mint[0]= System.in.read(double);.

< Answer >

21.The code fragment:

char ch = ' ';

try {

ch = (char) System.in.read();

while( (ch >= 'A') && (ch = 'A') && (ch = 'A') || (ch 'Z'));

} catch (Exception e) {

System.out.println("error found.");

}

(d)

char ch = ' ';

try {

do {

ch = (char) System.in.read();

} while ( (ch < 'A') || (ch > 'Z'));

} catch (Exception e) {

System.out.println("error found.");

}

(e)

char ch = ' ';

try {

do {

ch = (char) System.in.read();

} while ( (ch = 'Z'));

} catch (Exception e) {

System.out.println("error found.");

}.

< Answer >

22.What will the following program print when it is executed?

public class Practice11 {

public static void main(String args[]) {

int i = 0;

while (i < 3) {

if( i++ == 0 ) System.out.print("Merry");

if( i == 1) System.out.print("Merr");

if( ++i == 2) System.out.print("Mer");

else System.out.print("Oh no!");

}

}

}

(a)

Merry

Merr

Mer(b)

MerryMerrMerOh no!(c)

MerrMerOh no!(d)

MerryMerrMerOh no!

MerrMerOh no!

MerOh no!

(e)

Merry.

< Answer >

23.Which of the following is true?

(a)

Any applet must be an instance of java.awt.Applet.

(b)

You must always provide a no-arg constuctor for an applet.

(c)

You must always provide a main method for an applet.

(d)

You must always override the init method in an applet.

(e)

You must always overload the init method in an applet.

< Answer >

24.Which method that executes immediately after the init () method in an applet?

(a)

destroy()

(b)

start()

(c)

stop()

(d)

run()

(e)

exit().

< Answer >

25.When you run an applet, which of the following is invoked first?

(a)

The init method

(b)

The applet's default constructor

(c)

The stop method

(d)

The destroy method

(e)

Start method.

< Answer >

26.When you run the following applet from a browser, what is displayed?

import javax.swing.*;

public class Test extends JApplet {

public Test() {

System.out.println("Default constructor is invoked");

}

public void init() {

System.out.println("Init method is invoked");

}

}

(a)

Default constructor is invoked, then Init method is invoked

(b)

Init method is invoked, then Default constructor is invoked

(c)

Default constructor is invoked

(d)

Init method is invoked

(e)

Default constructor is invoked twice.

< Answer >

27.What must A method declare to throw?

(a)

Unchecked exceptions

(b)

Checked exceptions

(c)

Error

(d)

RuntimeException

(e)

Compliation exception.

< Answer >

28.What information may be obtained from a ResultSetMetaData object?

(a)

Database URL and product name

(b)

JDBC driver name and version

(c)

Number of columns in the result set

(d)

Number of rows in the result set

(e)

Number of tables in the database.

< Answer >

29.Which of the following statements is true?

(a)

You may load multiple JDBC drivers in a program.

(b)

You may create multiple connections to a database.

(c)

You may create multiple statements from one connection.

(d)

You can send queries and update statements through a Statement object.

(e)

All of the above.

< Answer >

30.Which is a special file that contains information about the files packaged in a JAR file?

(a)

Class file

(b)

Source file

(c)

Text file

(d)

Manifest file

(e)

Image file.

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

Write a JAVA class Rectangle that has methods getArea() and getPerimeter(). A rectangle is defined by a width and height. All instance variables should be private. You should supply a single constructor that takes two parameters, accessor and mutator methods, and a toString method for all instance variables.

(10 marks)

< Answer >2.

What is the output of the following program?

public class Advice {

public final static int LITTLE_ADVICE = 0;

public final static int MORE_ADVICE = 1;

public final static int LOTS_OF_ADVICE = 2;

public static void main(String[] args) {

for (int some_advice_please = 0 ;

some_advice_please 3.

Write a JAVA method that takes a reference to an array of integers as a parameter and reverses the ordering of the array. (e.g. If the array passed in is {7,2,6,3,5} then it should be changed to {5,3,6,2,7}.)

(10 marks)

< Answer >4.

a.Write a JAVA method called divide which takes two integers as parameters, divides the first by the second and returns the result. Use a try/catch structure in your method (no credit for doing it with an if statement). Your method should attempt to perform the operation. In the case of a divide by zero error, have your method return -1

b.Would it be a good idea to replace the while loop with

while( true )

try {

processLine( inFile.readLine() );

} catch ( EOFException e ) {

break;

}

Why or why not?

(5 + 5 = 10 marks)

< Answer >5.

a.Describe in English what the following program does.

import java.applet.*;

import java.awt.*;

public class Mystery extends Applet {

private int x = 100, y = 100;

public void paint(Graphics g) {

for (int i = 0; i END OF SECTION B

Section C : Applied Theory (20 Marks)

This section consists of questions with serial number 6 - 7.

Answer all questions.

Marks are indicated against each question.

Do not spend more than 25 -30 minutes on section C.

6.

What is an Applet? What are the applications of applets? How to pass parameters to applets in HTML and Java? What are the methods of applets in java?

(10 marks)

7.

What are the steps involved in JDBC Program? What are the five steps involved in calling a PL/SQL Function within a JDBC application?

(10 marks)

END OF SECTION C

END OF QUESTION PAPER

Suggested AnswersObject Oriented Programming and Java (MC221) : July 2007Section A : Basic Concepts1.Answer : (b)

Reason :Applets run from a web browser.< TOP >

2.Answer : (a)

Reason :Java is architecture neutral.< TOP >

3.Answer : (c)

Reason :public static void main(String[] args) the main method header is written in Java< TOP >

4.Answer : (a)

Reason :the extension name of a Java source code file .Java< TOP >

5.Answer : (e)

Reason :All are the reserved words of java.< TOP >

6.Answer : (e)

Reason :I nvalid call because char 'a' cannot be passed to string message< TOP >

7.Answer : (b)

Reason :This is known as ambiguous method invocation< TOP >

8.Answer : (a)

Reason :You cannot override the methods based on the type returned.< TOP >

9.Answer : (d)

Reason :rint returns the nearest even integer as a double since 3.5 is equally close to 3.0 and 4.0.< TOP >

10.Answer : (c)

Reason :After the for loop i is 6. x [5] is out of bounds.< TOP >

11.Answer : (a)

Reason :The contents of the array oldList have not been changed as result of invoking the reverse method.< TOP >

12.Answer : (d)

Reason :If a variable is declared as an int, it can't have a decimal component. So the / is integer division which returns a whole number with any decimal component dropped. Example:

int i = 7;

int j = 10;

int k;

k = j/i; // so k == 1

% (modulus operator). Returns the remainder of division. Given the example above, j % i == 3.

= versus ==. The assignment operator (=) assigns a value to a variable, but the comparison operator (==) returns a boolean result based on the comparison. If two variables are being tested for equality, == would be used. So, we use == in conditional statements like if(), while() and do--while().

Precedence. Pay attention to precedence. Have your precedence table handy at the exam< TOP >

13.Answer : (b)

Reason :Step through the program the way a computer would. As you step through, write down all the variables and their values as they change. DO NOT TRY TO DO IT ALL IN YOUR HEAD.< TOP >

14.Answer : (d)

Reason :The formal property of while(), for() and do--while() guarantees that the conditional is no longer true.

System.in.read() returns an int that is the ASCII value of the single character read from the keyboard stream. In ASCII, 'a' to 'z' and 'A' to 'Z' appear in consecutive, increasing order, with uppercase letters appearing before lowercase letters. Recall ('A' == 65) and ('a' == 97). Even though characters are written with single quotation marks, their underlying ASCII values can be manipulated as integers; for example, ('b' == 'a' + 1) and ('c' == 'a' + 2).< TOP >

15.Answer : (b)

Reason :Logic operators [&& (and), || (or), ! (not)]. Keep precedence in mind while doing these types of problems.

DeMorgan's Law. Steps are:

1.negate the entire expression;

2.negatate each of the 2 subexpressions;

3.change the && to || or || to &&.< TOP >

16.Answer : (c)

Reason :Notice the use of the word "possibly". Work through each option.< TOP >

17.Answer : (c)

Reason :Test cases should always be around any boundary conditions and should include extreme values.< TOP >

18.Answer : (d)

Reason :int)(Math.random() * 100) % n) is a handy tool that provides a random number in the range from 0 to n-1, where n

19.Answer : (e)

Reason :The statement to access a method of an object we write . is false.< TOP >

20.Answer : (e)

Reason :mint[0]= System.in.read(double); is an incorrect usage of System.in.read() with this array.< TOP >

21.Answer : (a)

Reason :Know the difference between while() and do--while(). Also, make sure you know the syntax of do--while().< TOP >

22.Answer : (b)

Reason :This program exercises your ability to walk through if() statements as well as post and pre increment. Be sure to carefully walk through the code and don't forget the while() loop.< TOP >

23.Answer : (a)

Reason :Any applet must be an instance of java.awt.Applet except this statement remaining all are false.< TOP >

24.Answer : (b)

Reason :start() method that executes immediately after the init() method in an applet< TOP >

25.Answer : (b)

Reason :When the applet is loaded to the Web browser, the Web browser creates an instance of the applet by invoking the applet?s default constructor.< TOP >

26.Answer : (a)

Reason :When the applet is loaded to the Web browser, the Web browser first creates an instance of the applet by invoking the applet?s default constructor, and then invokes the init() method< TOP >

27.Answer : (b)

Reason :A method must declare to throw checked options.< TOP >

28.Answer : (c)

Reason :Number of columns in the resultset information may be obtained from a ResultSetMetaData object.< TOP >

29.Answer : (e)

Reason :All the given statements are with respect to JDBC.< TOP >

30.Answer : (d)

Reason :Manifest file is a special file that contains information about the files packaged in a JAR file< TOP >

Section B : Problems1.

public class Rectangle {

private double width, height ; // integers ok

public Rectangle(double w, double h) {

width = w;

height = w;

}

public void setwidth(double w) { width = w; }

public double getwidth() { return width; }

public void setheight(double h) { height = h; }

public double getheight() { return height; }

public double getPerimeter() {

return 2 * width + 2 * height;

}

public double getArea() {

return width * height ;

}

public String toString() {

// any string that returns the data ok

return Rectangle: width == + width

, height == + height ;

}

}

1 mark for class definition

1 mark for variable declaration

1 mark for constructor

1 points for each accessor/mutator (4 total)

1 points for toString

1 point each for Area/perimeter (2 total)

(can loose one mark for bad punctuation)< TOP >2.

Speak no evil

Hear no evil

Speak no evil

See no evil

Hear no evil

Speak no evil

No advice< TOP >3.

public static void reverse(int [] a) {

for (int i = 0 ; i < a.length / 2; i++) {

int temp = a[i];

a[i] = a[a.length 1 i];

a[a.length 1 i] = temp;

}

}

Write a Java method using nested iteration that takes a positive

integer x as a parameter and prints a multiplication table from 1

times 1 up to x times x to System.out.

public static void mtable (int x) {

for (int i = 1 ; i 5.

a.Solution: This program draws 10 or 11 nested squares, depending on whether you include the central square as 1 of the squares.

b.A single pixel. We will also accept 0 pixels as a reasonable answer.

c.int i = 0;

while ( i