1.What is output by the following code? public static void stuff(int w) { w -= 2
ID: 3577669 • Letter: 1
Question
1.What is output by the following code?
public static void stuff(int w) {
w -= 2;
}
public static void main(String a[]) {
int n = 2;
stuff(n);
System.out.print(n);
}
0
1
2
3
4
2.Consider the following methods:
public static void printSport(double n) {
System.out.print("football ");
printSport((int)(n));
}
public static void printSport(int n) {
System.out.print("basketball ");
}
What is output by the method call printSport(8)?
football basketball
football football basketball basketball
football
basketball
basketball football
Consider the following method:
static void nPrint(String a, int n) {
System.out.println(a.charAt(n));
}
3.What potential error could this method cause?
Compile time error - incompatible types
Run time error - index out of range
Compile time error - index out of range
Run time error - incompatible types
Logic Error
4.What is output by the following code?
int a [] = {64 , 66 , 67 , 37 , 73 , 70 , 95 , 52 , 81 , 82};
for (int i = 0; i < a.length; i++) {
a[i] = a[i] / 10;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
2 4 5 2 2 8 6 8 1 2
4 6 7 7 3 0 5 2 1 2
6 6 6 3 7 7 9 5 8 8
7 7 7 4 8 8 10 6 9 9
5 7 8 8 4 1 6 3 2 3
5.What does the following algorithm do?
public static void mystery(int nums[]) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 != 0)
nums[i]++;
}
}
Adds 1 to every value in the array.
Doubles all the values in the array.
Changes all the values in the array to even numbers.
Tests if the elements in the array are even or odd.
Doubles every other value in the array.
Consider the following methods:
public static double average(int nums[]) {
int sum =0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
return (1.0 * sum) / nums.length;
} //average
public static int[] mystery(String a[]) {
int temp [] = new int[a.length];
for (int i = 0; i < a.length; i++) {
temp[i] = a[i].length();
}
return temp;
} //mystery
6.What is output by running the following?
String spelling[] = {"against", "forms", "belief", "government", "democratic", "movement", "understanding", "single", "followed", "scenario"};
System.out.println( average( mystery(spelling)));
8
8.1
8.5
10
Error, you cannot average Strings.
When you pass an array to a method, the method receives ______.
a copy of the array
a copy of the first element
a copy of the reference to the first element
a copy of the reference to the array
the length of the array
Which method(s) would produce the following output if they were passed the parameter, "hamster"?
hamster
hamste
hamst
hams
ham
ha
h
I.
public static void mystery(String wo) {
System.out.println(wo);
if (wo.length() > 0)
mystery( wo.substring(0, wo.length() - 1));
}
II.
public static void mystery(String wo) {
if (wo.length() > 0)
mystery( wo.substring(0, wo.length() - 1));
System.out.println(wo);
}
III.
public static void mystery(String wo) {
if (wo.length() > 0)
mystery( wo.substring( wo.length() - 1));
System.out.println(wo);
}
I only
II only
III only
I and III only
I, II and III
Consider a method defined with the header:
public static void doStuff(int x)
Which of the following method calls is legal?
doStuff(9);
doStuff(0.555);
doStuff(0.1 + 0.2);
doStuff(0.1, 0.2);
all of the above are legal except for D
Consider the following method checking whether a student has passing grades:
public boolean isPassing(int finalExam, int cumulativeAverage) {
/* Missing Code */
}
A student can pass a class if one of the following is true:
The final exam is at least 98
The cumulative average is over 60
Which of the following correctly replaces /* Missing Code */ so that the method works as intended?
I.
if ((finalExam >= 98) || (cumulativeAverage > 60))
return true;
return false;
II.
boolean pass = false;
if (finalExam >= 98)
pass = true;
if (cumulativeAverage > 60)
pass = true;
return pass;
III.
if (finalExam >= 98 && cumulativeAverage >= 60)
return true;
return false;
I only
II only
III only
I and II only
II and III only
What is output by the following code?
String q = "adjective";
String r = "stinky";
System.out.println( q.charAt( r.indexOf ('t')));
1
2
a
d
Index out of bounds exception
Explanation / Answer
Answer 1 : 2
Answer 2 : basketball
Answr 3 :run time error index out of range
Answr 4 : option 3
Answr 5 : change all elements to even numbers
Answer 6 : option 2 (81/10 = 8.1)
Answr 7 : a copy of the array
Answr 8:one only
Answer 9: option A
Answer 10: one and two only
Answer 11: option4 ( d )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.