1. Consider the following code segment. What are the values of num1 and num2 after the
while loop completes its execution? (See the benefit of short-circuit evaluation?)
int num1 = 0;
int num2 = 3;
while ((num2 != 0) && ((num1 / num2) >= 0))
{
num1 = num1 + 2;
num2 = num2 – 1;
}
2. What is the output of the program below?
public static void guess(int x, int y, String str)
{
int one = 3;
x = 9 – y;
y = y + 1;
x = x + one * one;
str += ” mine”;
System.out.println(x + ” ” + y + ” ” + one + ” ” + str);
}
public static void main(String[] args)
{
int one = 5, two = 7;
String name = “me”;
System.out.println(one + ” ” + two + ” ” + name);
guess(two, one, name);
System.out.println(one + ” ” + two + ” ” + name);
}
3. What is the output of the program below?
public static void what(int a, int b)
{
int x = 3;
a = 9 – b;
b = b + 1;
System.out.println(a + ” ” + b + ” ” + x);
}
public static void main(String[] args)
{
int x = 2, y = 4;
System.out.println(x + ” ” + y);
what(x, y);
System.out.println(x + ” ” + y);
}
4. Consider the following code segment. What will be the output?
int k = 1;
while (k < 20)
{
if ((k % 3) == 1)
{
System.out.print(k + ” “);
}
k++;
}
5. Consider the following class. What will be the output when the main method runs?
public class Mystery
{
private int test;
public Mystery()
{
test = 30;
}
public void one()
{
int test = 20;
System.out.println(“1-test = ” + test);
test++;
}
public void two()
{
System.out.println(“2-test = ” + test);
test++;
}
public void three(int test)
{
System.out.println(“3-test = ” + test);
test++;
}
public static void main (String[ ] args)
{
int test = 10;
Mystery aTest = new Mystery();
System.out.println(“main: test = ” + test);
aTest.one();
aTest.two();
aTest.three(40);
System.out.println(“main: test = ” + test);
}
}
Multiple Choice
1. The following method is supposed to calculate and return the sum of the numbers
from 1 to n,
where n > 0.
public static int sumToN(int n)
{
int sum = 0;
int count = 0;
while (count < n)
{
sum += count;
count++;
}
return sum;
}
Which statement best describes what method sumToN returns?
A. The value returned will be the sum of the numbers from 1 to n, inclusive.
B. The value returned will be the sum of the numbers from 0 to n – 1, inclusive.
C. The value returned will be 0.
D. The value returned will be n.
E. No value will be returned because the while loop is an infinite loop.
2. Refer to the following methods.
public static int add(int x, int y)
{
return x + y;
}
public static int multiply(int x, int y)
{
return x * y;
}
What is the value of the expression multiply(3, add(4, 5)) ?
A. 12
B. 17
C. 23
D. 27
E. 60
3. Consider the following code segment. What will be printed?
String s = “How do you do?”;
int index = s.indexOf(“o”);
while(index >= 0)
{
System.out.println(index + ” “);
s = s.substring(index + 1);
index = s.indexOf(“o”);
}
A. 1 3 2 3
B. 2 4 3 4
C. 1 5 8 12
D. 2 6 9 13
E. An IndexOutOfBoundsException is thrown.
4. Consider the following methods.
public void changer(String x, int y)
{
x = x + “peace”;
y = y * 2;
}
public void test()
{
String s = “world”;
int n = 6;
changer(s, n);
/* End of method */
}
When the call test() is executed, what are the values of s and n at the point indicated by
/* End of method */ ?
s n
A. world 6
B. worldpeace 6
C. world 12
D. worldpeace 12
E. peace 12
5. Consider the following class.
public class IntCell
{
private int value;
public int getValue()
{
return value;
}
public String toString()
{
return “” + value;
}
}
Assume that the following code segment appears in client code.
IntCell m = new IntCell();
Which of these statements can be used in the client class?
I. System.out.println(m.getValue());
II. System.out.println(m.value);
III. System.out.println(m);
A. I only
B. II only
C. III only
D. I and II
E. I and III

All papers are written by ENL (US, UK, AUSTRALIA) writers with vast experience in the field. We perform a quality assessment on all orders before submitting them.

Do you have an urgent order?  We have more than enough writers who will ensure that your order is delivered on time. 

We provide plagiarism reports for all our custom written papers. All papers are written from scratch.

24/7 Customer Support

Contact us anytime, any day, via any means if you need any help. You can use the Live Chat, email, or our provided phone number anytime.

We will not disclose the nature of our services or any information you provide to a third party.

Assignment Help Services
Money-Back Guarantee

Get your money back if your paper is not delivered on time or if your instructions are not followed.

We Guarantee the Best Grades
Assignment Help Services