What is output by the following code? System.out.print(21.0/5); What is output?
ID: 3577656 • Letter: W
Question
What is output by the following code?
System.out.print(21.0/5);
What is output?
3.5
4
4.2
5
5.5
Assume the following method has been defined:
public static int mystery(String a[], int x) {
int c = 0;
for (int i = 0; i < a.length; i++) {
if (a[i].length() > x)
c++;
}
return c;
}
What is output by the following code?
String b [] = {"aardvark", "banana", "cougar", "daikon", "elephant", "fog", "get"};
System.out.println(mystery(b, 5));
0
2
3
4
5
What does the following method do?
public static int mystery(int a[], int x) {
int c = 0;
for(int i = 0; i < a.length; i++) {
if (a[i] == x)
c++;
}
return c;
}
Returns a count of the number of elements in the array.
Returns the sum of the values in the array.
Returns a count of the number of times x appears in the array.
Returns the average of the values in the array.
Returns a count of the number of positive elements in the array.
Consider the following recursive method:
public static int recur(int x) {
if (x >= 0)
return x + recur(x - 1);
return 0;
}
What is returned by the method call recur(9)?
0
44
45
46
47
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
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
What does the following algorithm do?
public static boolean mystery(int nums[]) {
for (int i = 1; i < nums.length; i++)
if (nums[i - 1] >= nums[i])
return false;
return true;
}
Returns true if each element of the array is less than the element before.
Returns true if each element of the array is greater than or equal to the element before.
Returns true if each element of the array is greater than the element before.
Returns false if each element of the array is greater than or equal to the element before.
Returns false if each element of the array is greater than the element before.
Consider the following method:
static void nPrint(String a, int n) {
System.out.println(a.charAt(n));
}
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
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
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
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.
The following is intended to return the location of the first instance of the String the user enters from the keyboard, -1 if not found.
String names [] = new String[20];
//assume array is initialized
System.out.println(“Enter a name to search for: “);
String lookingFor = scan.nextLine();
int found = -1;
for (int i = 0; i < names.length; i++) {
if ( /* Missing Code */ ) {
found = i;
break;
}
}
Which of the following could replace /* Missing Code* / so that it works as intended?
! lookingFor.equals(names[i])
lookingFor.equals(names[i])
lookingFor [i].equals(names[i])
lookingFor != names[i]
lookingFor.equals names)
Given the following method definition:
public static int mystery(int a[]) {
int m = a[0];
for (int i = 0; i < a. length; i++) {
if (m > a[i])
m = a[i];
}
return m;
}
What would be returned by mystery if it was passed the following array?
int a[] = {34 , 18 , 34 , 38 , 27 , 37 , 39 , 21 , 19};
18
19
27
34
39
What return statement may be used in p()?
public static int[] p() {
// …
}
return 1;
return {1, 2, 3};
return int[]{1, 2, 3};
return new int[]{1, 2, 3};
return true
What mistake is in the following code?
public static double mystery (double a) {
System.out.println(a * 3.14);
}
It should say return true;
There should not be a return statement.
The parameter should be a boolean type.
The double should be changed to void since the method does not return a value.
The method cannot return a double.
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
What is output by the following code?
System.out.print(21.0/5);
What is output? 21.0/5 will print the value 4.2
Assume the following method has been defined:
public static int mystery(String a[], int x) {
int c = 0; //c is initialized to 0.
for (int i = 0; i < a.length; i++) { //for each value of the array a.
if (a[i].length() > x) //If that string length is greater than x value.
c++; //Increment the counter c.
}
return c; //Returns the c.
}
What is output by the following code?
Given an array of strings and an integer as input to the function, will return the number of
strings greater than specified length.
String b [] = {"aardvark", "banana", "cougar", "daikon", "elephant", "fog", "get"};
System.out.println(mystery(b, 5));
So, this code will print 5 as output.
What does the following method do?
public static int mystery(int a[], int x) {
int c = 0; //c is initialized to 0.
for(int i = 0; i < a.length; i++) { //for each value of the array a.
if (a[i] == x) //If that value is equal to x value.
c++; //Increment the counter c.
}
return c; //Returns the c.
}
Returns a count of the number of times x appears in the array.
Consider the following recursive method:
public static int recur(int x) {
if (x >= 0) //If the value is positive.
return x + recur(x - 1); //Add that value to recur(x-1).
return 0; //If not return 0.
}
//Which means, the function returns the sum of first x elements in the array.
What is returned by the method call recur(9)? 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 = 45.
So, the output is: 45.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.