Consider the following method: public static boolean tryIt (int a, int b) { if (
ID: 3572669 • Letter: C
Question
Consider the following method:
public static boolean tryIt (int a, int b) {
if ( (a - b) == Math.abs(a - b))
return true;
return false;
}
Which of the following methods produce the same results?
I.
public static boolean tryIt (int a, int b) {
if (a > b)
return true;
return false;
}
II.
public static boolean tryIt (int a, int b) {
if (a == b)
return true;
return false;
}
III.
public static boolean tryIt (int a, int b) {
if (a >= b)
return true;
return false;
}
I only
II only
III only
I and II
I, II, and III
Consider the following method:
public static void doStuff (int a[], int b) {
if (/* Missing Code */) {
for (int i = b; i < a.length; i++) {
a[i] = a[i] * 2;
}
}
}
What could be used to replace / *Missing Code* / so that there is no out of bounds exception?
b < a.length
b > 0 b < a.length
b > 0 && b <= a.length
b >= 0 || b < a.length
b >= 0 && b < a.length
Which of the following correctly gives random numbers between -20 and -11 inclusive?
int n = (int)(Math.random() * 20) - 10;
int n = (int)(Math.random() * 21);
int n = (int)(Math.random() * 11) - 20;
int n = (int)(Math.random() * 10) - 20;
int n = (int)(Math.random() * 10) - 21;
Consider a method defined with the header:
public static void doStuff(double a, int b)
Which of the following method calls is NOT legal?
int x = 9;
int y = 36;
doStuff (x, y);
doStuff (5.5, 7);
doStuff (5, 7);
doStuff (5, 7.5);
int z = 55;
doStuff (z, z + 1);
Consider the following variables and method representing a student.
private double gpa;
private int gradeLevel;
public boolean honorRoll()
{
/* Missing Code */
}
A student is placed on the honor role if their GPA is 3.5 or above, and they are in the 11th or 12th grade.
Which of the following correctly replaces /* missing code */ so that the method works as intended?
I.
if ((gpa >= 3.5) && ((gradeLevel == 11) || (gradeLevel == 12)))
return true;
return false;
II.
boolean pass = false;
if (gpa >= 3.5);
pass = true;
if ((gradeLevel == 11) || (gradeLevel == 12))
pass = true;
return pass;
III.
if ((gpa >= 3.5) || ((gradeLevel == 11) || (gradeLevel == 12)))
return true;
return false;
I only
II only
III only
II & III
I, II, & III
When a parameter is a(n) ______ data type, any changes made in a method are preserved.
int
binary
actual
class
primitive
What mistake is in the following code?
public static void mystery(double a) {
System.out.println(a * 3.14);
return a * 3.14;
}
It should say return true;
The return value should be a boolean.
The parameter should be a boolean type.
A method whose return type is void cannot return a value.
A method cannot return a double.
What return statement may be used in the following method?
public static int p() {
//...
}
return 1;
return {1, 2, 3};
return int[]{1, 2, 3};
return new int[]{1, 2, 3};
return p[1]
When you pass a double variable to a method, the method receives ______.
a copy of the memory address
the reference of the variable
the length of the variable
a copy of the variable
binary of the memory address
What is output by the following code?
String q = "onomatopoeia";
String r = "splat";
System.out.println( q.indexOf( r.charAt (3)));
4
5
12
a
t
Explanation / Answer
NOTE : always give numbers to your questions. Dont be in a hurry
I had marked the answers in the options :
answer 1>
Which of the following methods produce the same results?
I only
II only
III only ----> correct answer, because the purpose is to compare the values of a and b
I and II
I, II, and III
============================================================
answer 2>
What could be used to replace / *Missing Code* / so that there is no out of bounds exception?
b < a.length
b > 0 b < a.length
b > 0 && b <= a.length
b >= 0 || b < a.length --> correct answer, value of 'b' can't be negative but anything
b >= 0 && b < a.length
============================================================================
answer 3>
Which of the following correctly gives random numbers between -20 and -11 inclusive?
int n = (int)(Math.random() * 20) - 10;
int n = (int)(Math.random() * 21);
int n = (int)(Math.random() * 11) - 20;
int n = (int)(Math.random() * 10) - 20; --> correct answer;(int)(Math.random() * (max - min + 1) + min)
int n = (int)(Math.random() * 10) - 21;
=================================================================
answer 4>
Which of the following method calls is NOT legal?
int x = 9;
int y = 36;
doStuff (x, y);
doStuff (5.5, 7);
doStuff (5, 7);
doStuff (5, 7.5); ---> answer since 7.5 is not integer
int z = 55;
doStuff (z, z + 1);
===================================================================================
Answer 5>
A student is placed on the honor role if their GPA is 3.5 or above, and they are in the 11th or 12th grade.
Which of the following correctly replaces /* missing code */ so that the method works as intended?
I only ----> correct answer
II only
III only
II & III
I, II, & III
===================================================================
answer 6>
When a parameter is a(n) ______ data type, any changes made in a method are preserved.
int
binary
actual
class --> correct, because that will be pass by reference for the method
primitive
================================================================
answer 7>
What mistake is in the following code?
It should say return true;
The return value should be a boolean.
The parameter should be a boolean type.
A method whose return type is void cannot return a value. --> correct
A method cannot return a double.
==========================================================
answer 8>
What return statement may be used in the following method?
public static int p() {
//...
}
return 1; -->correct since the return type is int not int[]
return {1, 2, 3};
return int[]{1, 2, 3};
return new int[]{1, 2, 3};
return p[1]
=========================================================
answer 9>
When you pass a double variable to a method, the method receives ______.
a copy of the memory address
the reference of the variable
the length of the variable
a copy of the variable -->correct answer
binary of the memory address
====================================================
answer 10>
What is output by the following code?
String q = "onomatopoeia";
String r = "splat";
System.out.println( q.indexOf( r.charAt (3)));
4 --> correct. first index of 'a' is 4
5
12
a
t
NOTE: i would recommend the student to try to write & compile the code to get the concepts more clearer.
feel free to ask if you have any doubt :)