OSU CSE 2221 Final Review Exam Questions AND Correct
Answers
a - ✔✔After execution of the following code, what are the declared
type (static type) and the object type (dynamic type) of nCopy?
NaturalNumber n = new NaturalNumber2( );
NaturalNumber nCopy = new NaturalNumber1L(n);
*a.* declared type is NaturalNumber, object type is NaturalNumber1L
*b.* declared type is NaturalNumber, object type is NaturalNumber2
*c.* declared type is NaturalNumber2, object type is
NaturalNumber1L
*d.* declared type is NaturalNumber1L, object type is
NaturalNumber2
a - ✔✔Consider the following code fragment:
int x = 8;
while ((x / 3) != 2) { x = x - 1; }
How many times will the body of the loop execute?
*a.* 0
,*b.* 1
*c.* 2
*d.* 3
*e.* the loop will never terminate
a - ✔✔Given the following code, which statement is true?
SimpleWriter out = new SimpleWriter1L( );
NaturalNumber n1 = new NaturalNumber1L(5);
NaturalNumber n2 = n1;
n2.decrement( );
out.println("n1 = " + n1 + ", n2 = " + n2);
*a.* will output "n1 = 4, n2 = 4"
*b.* will output "n1 = 5, n2 = 4"
*c.* will not compile
*d.* will result in a run-time error
*e.* none of the above
a - ✔✔Provided by the code calling the method:
*a.* argument
,*b.* variable
*c.* formal parameter
*d.* constant
*e.* expression
a - ✔✔What can you say about these two methods m1 & m2?
private static boolean m1(int x, int y) {
boolean result;
if (x>0 && y > 0) { result = false; }
else { result = true; }
return result;
}
private static boolean m2(int x, int y)
{ return !(x > 0 && y > 0); }
*a.* both return the same result for all values of x and y
*b.* they return different values of x and y
*c.* m1 contains syntax errors
*d.* m2 contains syntax errors
*e.* both have syntax errors
, a - ✔✔What is the value of k after this code completes?
int i = 0, j = 0, k = 0;
while (i < 3) {
i++;
j = 0;
while (j < 3) {
j++;
k = k + i + j;
}}
*a.* 36
*b.* 54
*c.* 9
*d.* 6
a - ✔✔What is the value of x after this statement?
double x = ;
*a.* 0.0
Answers
a - ✔✔After execution of the following code, what are the declared
type (static type) and the object type (dynamic type) of nCopy?
NaturalNumber n = new NaturalNumber2( );
NaturalNumber nCopy = new NaturalNumber1L(n);
*a.* declared type is NaturalNumber, object type is NaturalNumber1L
*b.* declared type is NaturalNumber, object type is NaturalNumber2
*c.* declared type is NaturalNumber2, object type is
NaturalNumber1L
*d.* declared type is NaturalNumber1L, object type is
NaturalNumber2
a - ✔✔Consider the following code fragment:
int x = 8;
while ((x / 3) != 2) { x = x - 1; }
How many times will the body of the loop execute?
*a.* 0
,*b.* 1
*c.* 2
*d.* 3
*e.* the loop will never terminate
a - ✔✔Given the following code, which statement is true?
SimpleWriter out = new SimpleWriter1L( );
NaturalNumber n1 = new NaturalNumber1L(5);
NaturalNumber n2 = n1;
n2.decrement( );
out.println("n1 = " + n1 + ", n2 = " + n2);
*a.* will output "n1 = 4, n2 = 4"
*b.* will output "n1 = 5, n2 = 4"
*c.* will not compile
*d.* will result in a run-time error
*e.* none of the above
a - ✔✔Provided by the code calling the method:
*a.* argument
,*b.* variable
*c.* formal parameter
*d.* constant
*e.* expression
a - ✔✔What can you say about these two methods m1 & m2?
private static boolean m1(int x, int y) {
boolean result;
if (x>0 && y > 0) { result = false; }
else { result = true; }
return result;
}
private static boolean m2(int x, int y)
{ return !(x > 0 && y > 0); }
*a.* both return the same result for all values of x and y
*b.* they return different values of x and y
*c.* m1 contains syntax errors
*d.* m2 contains syntax errors
*e.* both have syntax errors
, a - ✔✔What is the value of k after this code completes?
int i = 0, j = 0, k = 0;
while (i < 3) {
i++;
j = 0;
while (j < 3) {
j++;
k = k + i + j;
}}
*a.* 36
*b.* 54
*c.* 9
*d.* 6
a - ✔✔What is the value of x after this statement?
double x = ;
*a.* 0.0