Consider the method total below. public static int total (int result, int a, int
ID: 3876154 • Letter: C
Question
Consider the method total below. public static int total (int result, int a, int b) { if (a==0) { if (b==0) { return result *2; } return result /2; } else { return result *3; } } The assignment statement x=total (5,0,1); must result in A. x being assigned the value 15 B. x being assigned the value 10 C. x being assigned the value 5 D. x being assigned the value 2 E. x being assigned the value 0
Consider the method total below. public static int total (int result, int a, int b) { if (a==0) { if (b==0) { return result *2; } return result /2; } else { return result *3; } } The assignment statement x=total (6,0,0); must result in A. x being assigned the value 8 B. x being assigned the value 4 C. x being assigned the value 5 D. x being assigned the value 12 E. x being assigned the value 10
Consider the following declarations. public class List { private static final int MAX_ITEMS = < some value >; private int[] items;
private int numItems;
//constructor(s) and other methods not shown
//precondition: 0<=numItems <MAX_LENGTH
public int find (int num)
{
boolean found = false;
int loc= 0
while(!found && loc< numItems)
{
if(items[loc] == num)
{
found = true;
}
loc++;
}
if (!found)
{
loc= -1;
}
return loc;
}
}
Which of the following is a correct postcondition for the find method?
returns true if num is found in this list; false otherwise.
returns true if num is found in this list; void othewise.
returns the number of items currently in this list.
returns -1 if num is not in list; index of num otheriwse.
returns index of num if num is in this list; nothing otherwise
What is the output of the program shown below? public class SomeClass
{
private int x, y;
public SomeClass (int xValue, int yValue)
{
x = xValue;
y= yValue;
}
public void m1()
{
x = 30;
System.out.print(x + "");
}
}
public class Tester
{
public static void main (String[] args)
{
int x = 20;
int y = 10;
SomeClass z = new Someclass(y, x);
z.m2();
z.m1();
}
}
21 10 21
21 30 21
11 30 11
11 30 21
Explanation / Answer
1)
option D is correct answer since a value is 0 and b value is non zero so return result/2 is evaluated i.e., 5/2=2 and x is assigned value 2
2)
option D is correct answer since a value is 0 and b value is zero so return result*2 is evaluated i.e., 6*2=12 and x is assigned value 12
3)
The correct postcondition for find() is according to given logic if given nuumber is not found in the list -1 is returned otherwise index of the number is returned
4)
first of all u will get compile time errors at the following statements
SomeClass z = new SomeClass(y, x);
z.m2(); (m2 defintion is not provided)
if z.m1 is considered the output of program is 30