CSE 2221 Final Exam Review| Study
Questions Solved 100% Correct
If you write class C implements interface I, the Java compiler checks:
a. Whether I overrides all the methods of C
b. Whether C inherits all the methods of I
c. Whether each method in I has a method body in C
d. Whether each method body in C is a correct implementation of the contract for that method
in I - ANSWER c. Whether each method in I has a method body in C
Consider the following code:
int x=80;
if (x>70){
x=40;
}else if (x<90){
x=20;
}
if (x>30){
x=60;
}
a. 20
b. 40
c. 60
d. 80 - ANSWER C. 60
In design-by-contract, the code responsible for making sure the precondition (requires clause)
is true when a method is called is:
,a. the code that implements the method
b. the client code that calls the method
c. both the client and the implementation code
d. neither the client nor the implementation code - ANSWER b. the client code that calls
the method
Suppose you want want to set teh double variable value to a random real number
uniformly distributed in the interval [-2.0, 2.0). You have made the following declaration:
Random r=new Random1L();
noting that r.nextDouble() returns a random real number uniformly distributed in the
interval [0.0, 1.0). Which statement will set value to the desired result?
a. value=-2.0 * r.nextDouble();
b. value=-2.0+2.0*r.nextdouble();
c. value= 4.0*r.nextDouble()-2.0;
d. value=-2.0 *r.nextDouble() + 2.0*r.nextDouble() - ANSWER c. value= 4.0*r.nextDouble()-2.0;
Consider the following method header:
private static boolean isPrime(int x)
and the call:
boolean answer=isPrime(number);
In this context:
a. x is a formal parameter and number is an argument(or actual parameter)
b. x is an argument (or actual parameter) and number is a formal parameter
c. x is a local variable and number is a distinguished variable
d. both x and number are global variables - ANSWER a. x is a formal parameter and number is
an argument(or actual parameter)
If a and b are boolean variables, when does the expression (!a||!b) evaluate to false?
a. when either a or b is true, but not both
, b. when both a and b are false
c. when both a and b are true
d. when a is not equal to b - ANSWER c. when both a and b are true
Consider the following method header:
private static void setToZero (int x)
and the client code:
int number=10;
setToZero(number);
The value of variable number after the method call:
a. is guaranteed to be 0
b. is guaranteed to be 10
c. could be either 0 or 10, but nothing else
d. could be anything; there is not enough information to know - ANSWER b. is guaranteed to
be 10
What is the value of nums[1] +nums[3] just after you declare:
int[] nums={7,3,5,6};
a.12
b.9
c.4
d. It is impossible to tell - ANSWER b.9
Which line of code could be the body of a method that reports whether a point
with coordinates (x,y) is inside the square with sides of length 2 centered (1,1)?
a. return(Math.abs(x)<=1.0 &&Math.abs(y)<=1.0);
b. return(x<=2.0 && y<=2.0);
c. return (Math.abs(x-1.0)<=1.0 && Math.abs(y-1.0)<==1.0)
Questions Solved 100% Correct
If you write class C implements interface I, the Java compiler checks:
a. Whether I overrides all the methods of C
b. Whether C inherits all the methods of I
c. Whether each method in I has a method body in C
d. Whether each method body in C is a correct implementation of the contract for that method
in I - ANSWER c. Whether each method in I has a method body in C
Consider the following code:
int x=80;
if (x>70){
x=40;
}else if (x<90){
x=20;
}
if (x>30){
x=60;
}
a. 20
b. 40
c. 60
d. 80 - ANSWER C. 60
In design-by-contract, the code responsible for making sure the precondition (requires clause)
is true when a method is called is:
,a. the code that implements the method
b. the client code that calls the method
c. both the client and the implementation code
d. neither the client nor the implementation code - ANSWER b. the client code that calls
the method
Suppose you want want to set teh double variable value to a random real number
uniformly distributed in the interval [-2.0, 2.0). You have made the following declaration:
Random r=new Random1L();
noting that r.nextDouble() returns a random real number uniformly distributed in the
interval [0.0, 1.0). Which statement will set value to the desired result?
a. value=-2.0 * r.nextDouble();
b. value=-2.0+2.0*r.nextdouble();
c. value= 4.0*r.nextDouble()-2.0;
d. value=-2.0 *r.nextDouble() + 2.0*r.nextDouble() - ANSWER c. value= 4.0*r.nextDouble()-2.0;
Consider the following method header:
private static boolean isPrime(int x)
and the call:
boolean answer=isPrime(number);
In this context:
a. x is a formal parameter and number is an argument(or actual parameter)
b. x is an argument (or actual parameter) and number is a formal parameter
c. x is a local variable and number is a distinguished variable
d. both x and number are global variables - ANSWER a. x is a formal parameter and number is
an argument(or actual parameter)
If a and b are boolean variables, when does the expression (!a||!b) evaluate to false?
a. when either a or b is true, but not both
, b. when both a and b are false
c. when both a and b are true
d. when a is not equal to b - ANSWER c. when both a and b are true
Consider the following method header:
private static void setToZero (int x)
and the client code:
int number=10;
setToZero(number);
The value of variable number after the method call:
a. is guaranteed to be 0
b. is guaranteed to be 10
c. could be either 0 or 10, but nothing else
d. could be anything; there is not enough information to know - ANSWER b. is guaranteed to
be 10
What is the value of nums[1] +nums[3] just after you declare:
int[] nums={7,3,5,6};
a.12
b.9
c.4
d. It is impossible to tell - ANSWER b.9
Which line of code could be the body of a method that reports whether a point
with coordinates (x,y) is inside the square with sides of length 2 centered (1,1)?
a. return(Math.abs(x)<=1.0 &&Math.abs(y)<=1.0);
b. return(x<=2.0 && y<=2.0);
c. return (Math.abs(x-1.0)<=1.0 && Math.abs(y-1.0)<==1.0)