WEEK-4
Q1. Suppose your method does not return any value, which of the following keywords can be used as a return type?
Select one:
A.
void
B.
int
C.
public
D.
double
E.
None of the above
Q2. Does the method call in the following method cause compile errors?
public static void main(String[] args) {
Math.pow(2, 4);
}
Select one:
A.
Yes
B.
No
Q3. You should fill in the blank in the following code with ________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is ");
printGrade(78.5);
System.out.print("The grade is ");
printGrade(59.5);
}
public static ________ printGrade(double score) {
if (score >= 90.0) {
System.out.println('A');
}
else if (score >= 80.0) {
System.out.println('B');
}
else if (score >= 70.0) {
System.out.println('C');
}
else if (score >= 60.0) {
System.out.println('D');
}
else {
System.out.println('F');
}
}
}
Select one:
A.
int
B.
double
C.
char
D.
void
E.
Q4. You should fill in the blank in the following code with ________.
public class Test {
public static void main(String[] args) {
System.out.print("The grade is " + getGrade(78.5));
System.out.print("\nThe grade is " + getGrade(59.5));
}
public static ________ getGrade(double score) {
if (score >= 90.0)
return 'A';
else if (score >= 80.0)
return 'B';
else if (score >= 70.0)
return 'C';
else if (score >= 60.0)
return 'D';
else
return 'F';
}
}
Select one:
A.
char
B.
void
C.
int
D.
double
E.
boolean
Q5. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is the output of the call nPrint('a', 4)?
Select one:
A.
aaa
B.
aaaa
C.
aaaaa
D.
invalid call
Q6. Given the following method
static void nPrint(String message, int n) {
while (n > 0) {
System.out.print(message);
n--;
}
}
What is k after invoking nPrint("A message", k)?
int k = 2;
nPrint("A message", k);
Select one:
A.
0
B.
1
C.
2
D.
Q7. Analyze the following code.
public class Test {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
System.out.println("max(int, double) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
System.out.println("max(double, int) is invoked");
if (num1 > num2)
return num1;
else
return num2;
}
}
Select one:
A.
The program runs and prints 2 followed by "max(double, int)"
is invoked.
B.
The program runs and prints 2 followed by "max(int, double)"
is invoked.
C.
The program cannot compile because you cannot have the print statement
in a non-void method.
D.
The program runs and prints "max(int, double) is invoked"
followed by 2.
E.
The program cannot compile
because the compiler cannot determine which max method should be invoked.
Q8. (char)('a' + Math.random() * ('z' - 'a' + 1)) returns a random character ________.
Select one:
A.
between 'b' and 'z'
B.
between 'b' and 'y'
C.
between 'a' and 'y'
D.
between 'a' and 'z'
Q9. Which of the following is the best for generating random integer 0 or 1?
Select one:
A.
(int)Math.random() + 1
B.
(int)(Math.random() + 0.5)
C.
(int)(Math.random() + 0.8)
D.
(int)Math.random()
E.
(int)(Math.random() + 0.2)
Q10. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as ________.
Select one or more:
A.
encapsulation
B.
simplifying method
C.
method hiding
D.
information hiding
Comments
Post a Comment