Object Oriented Programming and Java (MC221) July 2007
Object Oriented Programming and Java (MC221) July 2007
2.
3.
4.
5.
6.
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.
Which of the following language is Architecture-Neutral?
(a)
Java
(b)
C++
(c)
C
(d)
Ada
(e)
Pascal.
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).
What is the extension name of a Java source code file?
(a)
.java
(b)
.obj
(c)
.class
(d)
.exe
(e)
.javac.
Which of the following is not the reserved words in java?
(a)
Public
(b)
Static
(c)
Void
(d)
Class
(e)
Num.
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.
7.
8.
9.
The program has a syntax error because the two methods m have the same signature
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.
What is Math.rint(3.5)?
(a)
3.0
(b)
3
(c)
4
(d)
4.0
(e)
5.0.
10.
11.
12.
(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.
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.
13.
14.
15.
is equivalent to:
16.
17.
18.
19.
(a)
(! (b = 0)) && (! (c > 5))
(b)
(b == 0) && (c > 5)
(c)
(b != 0) && (c <= 5)
(d)
! ((b <> 0) && (c <= 5))
(e)
(b == 0) && (c <= 5).
Suppose a method signature looks like this:
public static void Check(char c, Funny f) { ... }
Which one of the following calls to method Check() could possibly be syntactically valid?
(a)
Check("a", x);
(b)
Check(97, x);
(c)
Check(sweat(a[3]), x);
(d)
Check(char q, Funny p);
(e)
Check(Funny p, char q);.
A proposed graduated state income tax applies the following rate schedule to net income after deductions:
0 --- if the net income is negative
2% --- on incomes less than $10,000
$200, plus 4% of excess over $10,000 --- for incomes between $10,000 and $20,000 (inclusive)
$600, plus 6% of excess over $20,000 --- for incomes between $20,000 and $30,000 (inclusive)
$1,200, plus 8% of excess over $30,000 --- for incomes greater than $30,000
You have been given a program that is supposed to calculate the tax on a given income in dollars. Which one of the
following is the best set of test data on which to try out this program?
(a)
it makes no difference, any of the following are equally good!
(b)
2000, 4000, 6000, 8000, 10000, 12000, 14000
(c)
-10, 75, 10000, 15000, 20000, 25000, 30000, 40000
(d)
0, 10000, 20000, 30000, 40000
(e)
-5000, 0, 5000, 15000, 25000, 35000.
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.
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 <method>.<object>.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
END OF SECTION A
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)
2.
case LOTS_OF_ADVICE:
System.out.println("See no evil");
case MORE_ADVICE:
System.out.println("Hear no evil");
case LITTLE_ADVICE:
System.out.println("Speak no evil");
break;
default:
System.out.println("No advice");
}
}
}
Give detailed explanation of the above code
(10 marks)
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)
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)
5.
(6 + 2 + 2 = 10 marks)
END OF SECTION B
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
<Answer >
<Answer >
Suggested Answers
Object Oriented Programming and Java (MC221) : July 2007
Section A : Basic Concepts
1.
Answer : (b)
Reason : Applets run from a web browser.
2.
Answer : (a)
Reason : Java is architecture neutral.
3.
Answer : (c)
Reason : public static void main(String[] args) the main method header is written in Java
4.
Answer : (a)
Reason : the extension name of a Java source code file .Java
5.
Answer : (e)
Reason : All are the reserved words of java.
6.
Answer : (e)
Reason : I nvalid call because char 'a' cannot be passed to string message
7.
Answer : (b)
Reason : This is known as ambiguous method invocation
8.
Answer : (a)
Reason : You cannot override the methods based on the type returned.
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.
10.
Answer : (c)
Reason : After the for loop i is 6. x [5] is out of bounds.
11.
Answer : (a)
Reason : The contents of the array oldList have not been changed as result of invoking the reverse method.
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
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.
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).
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 &&.
16.
Answer : (c)
Reason : Notice the use of the word "possibly". Work through each option.
17.
Answer : (c)
Reason : Test cases should always be around any boundary conditions and should include extreme values.
18.
Answer : (d)
Reason : int)(Math.random() * 100) % n) is a handy tool that provides a random number in the range from 0 to n1, where n <= 100.
Be sure to have your cast charts handy to know when an explicit cast is required and when it is not.
19.
Answer : (e)
Reason : The statement to access a method of an object we write <method>.<object> is false.
20.
Answer : (e)
Reason : mint[0]= System.in.read(double); is an incorrect usage of System.in.read() with this array.
21.
Answer : (a)
Reason : Know the difference between while() and do--while(). Also, make sure you know the syntax of do-while().
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.
23.
Answer : (a)
Reason : Any applet must be an instance of java.awt.Applet except this statement remaining all are false.
24.
Answer : (b)
Reason : start() method that executes immediately after the init() method in an applet
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.
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
27.
Answer : (b)
Reason : A method must declare to throw checked options.
28.
Answer : (c)
Reason : Number of columns in the resultset information may be obtained from a ResultSetMetaData object.
29.
Answer : (e)
Reason : All the given statements are with respect to JDBC.
30.
Answer : (d)
Reason : Manifest file is a special file that contains information about the files packaged in a JAR file
Section B : Problems
1.
Speak no evil
Hear no evil
Speak no evil
See no evil
Hear no evil
Speak no evil
No advice
3.
}
}
4.
a.
b.
5.
a.
b.
c.
Solution: This program draws 10 or 11 nested squares, depending on whether you include the central square
as 1 of the squares.
A single pixel. We will also accept 0 pixels as a reasonable answer.
int i = 0;
while ( i <= 200 ) {
g.drawRect(i, i, 400 - 2 * i, 400 - 2 * i);
i = i + 20;
}for loop in Mystery as a while loop.
int i = 0;
while ( i <= 200 ) {
g.drawRect(i, i, 400 - 2 * i, 400 - 2 * i);
i = i + 20;
}
6.
7.
8.
9.
10.
1.)
2.)
3.)
4.)
5.)