Java Programming OOPs
Questions 131 to 140
131.
|
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;
}
}
|
||||||||||
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:
|
|||||||||||
The Java
program:
public
class Practice2 {
public
static void main(String args[]) {
for(int
row = -2; row <= 2; row++) {
for(int
col = -2; col <= 2; col++)
if(
col*col < row*row) System.out.print("* ");
else
System.out.print(". ");
System.out.println();
}
}
}
will
produce which one of the following patterns when it is executed?
|
|||||||||||
Suppose the
following Java statements:
char ch
= ' ';
try {
do
{
ch
= (char) System.in.read();
}
while ( ch != 'G');
}
catch(Exception i) {
System.out.println(ch);
}
are
executed and applied to the following input which is typed on the keyboard:
g MnGyZ
The
final value of the variable ch will be:
|
|||||||||||
The Java
expression:
! ((b
!= 0) || (c <= 5))
is
equivalent to:
|
|||||||||||
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 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?
|
|||||||||||
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?
|
|||||||||||
Which of
the following statement is false?
|
|||||||||||
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?
|
Answers
131.
|
Answer : (a)
Reason : The
contents of the array oldList have not been changed as result of invoking the
reverse method.
|
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
|
|
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.
|
|
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).
|
|
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
&&.
|
|
Answer : (c)
Reason : Notice
the use of the word "possibly". Work through each option.
|
|
Answer : (c)
Reason : Test
cases should always be around any boundary conditions and should include
extreme values.
|
|
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 <= 100.
Be
sure to have your cast charts handy to know when an explicit cast is required
and when it is not.
|
|
Answer : (e)
Reason : The
statement to access a method of an
object we write <method>.<object> is false.
|
|
Answer : (e)
Reason : mint[0]=
System.in.read(double); is an incorrect
usage of System.in.read() with this array.
|
thank you.
ReplyDeletejava tutorial
java tutorial