Java Programming Questions and Answers Set 14

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;
}
}
(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.
132.
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.
133.
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?
(a)
. . . . .
. . * . .
. * * * .
. . * . .
. . . . .
(b)
. * * * .
. . * . .
. . . . .
. . * . .
. * * * .
(c)
. . * . .
. * * * .
* * * * *
. * * * .
. . * . .
(d)
* * . * *
* . . . *
. . . . .
* . . . *
* * . * *
(e)
. . . . .
* . . . .
* * . . .
* * * . .
* * * * ..
134.
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:
(a)
'g'
(b)
' '
(c)
'n'
(d)
'G'
(e)
'y'.
135.
The Java expression:
! ((b != 0) || (c <= 5))
is equivalent to:
(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).
136.
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);.
137.
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.
138.
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.
139.
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>.
140.
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);.




Answers



131.
Answer : (a)
Reason  :       The contents of the array oldList have not been changed as result of invoking the reverse method.
132.
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
133.
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.
134.
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).
135.
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 &&.
136.
Answer : (c)
Reason  :       Notice the use of the word "possibly". Work through each option.
137.
Answer : (c)
Reason  :       Test cases should always be around any boundary conditions and should include extreme values.
138.
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.
139.
Answer : (e)
Reason  :       The statement  to access a method of an object we write <method>.<object> is false.
140.
Answer : (e)
Reason  :       mint[0]= System.in.read(double); is an incorrect usage of System.in.read() with this array.



<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


1 comment :