Deck 9: Strings
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/25
Play
Full screen (f)
Deck 9: Strings
1
How many elements are in array double[] list = new double[5]? B. 4
C) 6
D) 0
C) 6
D) 0
A
2
In Java, the word true is ________. B. a Java keyword
C) same as value 0
D) same as value 1
C) same as value 0
D) same as value 1
A
3
How many times will the following code print "Welcome to Java"? int count = 0;
Do {
System.out.println("Welcome to Java");
} while (count++ < 10);
A) 10
B) 8
C) 0
D) 9
Do {
System.out.println("Welcome to Java");
} while (count++ < 10);
A) 10
B) 8
C) 0
D) 9
E
4
24 % 5 is _____ A. 2
B) 0
D) 1
E) 3
B) 0
D) 1
E) 3
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
4
The "less than or equal to" comparison operator in Java is __________. A. <<
B) =<
C) !=
E) <
B) =<
C) !=
E) <
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
5
What is the result of 45 / 4? B. 12
C) 10
D) 11.25
C) 10
D) 11.25
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
6
Write the following method that returns true if the list is already sorted in increasing order.
public static boolean isSorted(int[] list)
Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list.
Enter list: 8 10 1 5 16 61 9 11 1
The list is not sorted
Enter list: 10 1 1 3 4 4 5 7 9 11 21
The list is already sorted
Here is the outline of the program:
public class Test {
public static void main(String[] args) {
// Fill in the code here
}
public static boolean isSorted(int[] list) {
// Fill in the code here
}
}
public static boolean isSorted(int[] list)
Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list.
Enter list: 8 10 1 5 16 61 9 11 1
The list is not sorted
Enter list: 10 1 1 3 4 4 5 7 9 11 21
The list is already sorted
Here is the outline of the program:
public class Test {
public static void main(String[] args) {
// Fill in the code here
}
public static boolean isSorted(int[] list) {
// Fill in the code here
}
}
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
7
Analyze the following code: public class Test {
Public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
Public static int xMethod(int n, long l) {
System.out.println("int, long");
Return n;
}
Public static long xMethod(long n, long l) {
System.out.println("long, long");
Return n;
}
}
A) The program does not compile because the compiler cannot distinguish which xmethod to invoke.
B) The program runs fine but displays things other than 5.
C) The program displays long, long followed by 5.
D) The program displays int, long followed by 5.
Public static void main(String[] args) {
System.out.println(xMethod(5, 500L));
}
Public static int xMethod(int n, long l) {
System.out.println("int, long");
Return n;
}
Public static long xMethod(long n, long l) {
System.out.println("long, long");
Return n;
}
}
A) The program does not compile because the compiler cannot distinguish which xmethod to invoke.
B) The program runs fine but displays things other than 5.
C) The program displays long, long followed by 5.
D) The program displays int, long followed by 5.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
8
What is the value in count after the following loop is executed? int count = 0;
Do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
A) 11
B) 0
C) 9
D) 8
Do {
System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
A) 11
B) 0
C) 9
D) 8
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
9
If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value. A. int
B) long
C) byte
D) double
B) long
C) byte
D) double
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
10
What is i after the following for loop? int y = 0;
For (int i = 0; i<10; ++i) {
Y += i;
}
A) 9
B) 10
D) 11
For (int i = 0; i<10; ++i) {
Y += i;
}
A) 9
B) 10
D) 11
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
11
What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) {
// iteration
}
A) 2*n
C) n - 1
D) n + 1
// iteration
}
A) 2*n
C) n - 1
D) n + 1
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
12
Write a method to display a pattern as follows:
1
1 2
1 2 3
...
...
1 2 3 4 5 ... n
The method header is
public static void displayPattern(int n)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter the side of the pentagon
System.out.print("Enter n: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n) {
// Fill in the code here
}
}
1
1 2
1 2 3
...
...
1 2 3 4 5 ... n
The method header is
public static void displayPattern(int n)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter the side of the pentagon
System.out.print("Enter n: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n) {
// Fill in the code here
}
}
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
13
What is Math.floor(3.6)? A. 5.0
B) 3
D) 4
B) 3
D) 4
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
14
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;
}
}
A) The program runs and prints 2 followed by "max(double, int)" is invoked.
B) The program cannot compile because the compiler cannot determine which max method should be invoked.
C) The program runs and prints "max(int, double) is invoked" followed by 2.
D) The program runs and prints 2 followed by "max(int, double)" is invoked.
E) The program cannot compile because you cannot have the print statement in a non-void method.
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;
}
}
A) The program runs and prints 2 followed by "max(double, int)" is invoked.
B) The program cannot compile because the compiler cannot determine which max method should be invoked.
C) The program runs and prints "max(int, double) is invoked" followed by 2.
D) The program runs and prints 2 followed by "max(int, double)" is invoked.
E) The program cannot compile because you cannot have the print statement in a non-void method.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
15
__________ is to implement one method in the structure chart at a time from the top to the bottom. A. Stepwise refinement
C) Bottom-up and top-down approach
D) Bottom-up approach
C) Bottom-up and top-down approach
D) Bottom-up approach
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
16
What is the output for y? int y = 0;
For (int i = 0; i<10; ++i) {
Y += i;
}
System.out.println(y);
B) 12
C) 13
D) 10
E) 11
For (int i = 0; i<10; ++i) {
Y += i;
}
System.out.println(y);
B) 12
C) 13
D) 10
E) 11
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
17
The following code displays ___________. double temperature = 50;
If (temperature >= 100)
System.out.println("too hot");
Else if (temperature <= 40)
System.out.println("too cold");
Else
System.out.println("just right");
A) too hot
B) too cold
C) too hot too cold just right
If (temperature >= 100)
System.out.println("too hot");
Else if (temperature <= 40)
System.out.println("too cold");
Else
System.out.println("just right");
A) too hot
B) too cold
C) too hot too cold just right
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
18
Analyze the following code fragments that assign a boolean value to the variable even. Code 1:
If (number % 2 == 0)
Even = true;
Else
Even = false;
Code 2:
Even = (number % 2 == 0) ? true: false;
Code 3:
Even = number % 2 == 0;
A) Code 3 has a syntax error, because you attempt to assign number to even.
B) Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression.
C) All three are correct, but Code 1 is preferred.
D) All three are correct, but Code 2 is preferred.
E) All three are correct, but Code 3 is preferred.
If (number % 2 == 0)
Even = true;
Else
Even = false;
Code 2:
Even = (number % 2 == 0) ? true: false;
Code 3:
Even = number % 2 == 0;
A) Code 3 has a syntax error, because you attempt to assign number to even.
B) Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression.
C) All three are correct, but Code 1 is preferred.
D) All three are correct, but Code 2 is preferred.
E) All three are correct, but Code 3 is preferred.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
19
Write a program that prompts the user to enter an integer n (assume n >= 2) and displays its largest factor other than itself.
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
20
The reverse method is defined in the textbook. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6};
List1 = reverse(list1);
B) list1 is 1 2 3 4 5 6
C) list1 is 0 0 0 0 0 0
D) list1 is 6 6 6 6 6 6
List1 = reverse(list1);
B) list1 is 1 2 3 4 5 6
C) list1 is 0 0 0 0 0 0
D) list1 is 6 6 6 6 6 6
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
21
Suppose a method p has the following heading: public static int[][] p()
What return statement may be used in p()?
A) return 1;
B) return {1, 2, 3};
C) return int[]{1, 2, 3};
D) return new int[]{1, 2, 3};
E) return new int[][]{{1, 2, 3}, {2, 4, 5}};
What return statement may be used in p()?
A) return 1;
B) return {1, 2, 3};
C) return int[]{1, 2, 3};
D) return new int[]{1, 2, 3};
E) return new int[][]{{1, 2, 3}, {2, 4, 5}};
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
22
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.
A) 3.4
B) 2.0
C) 3.5
D) 5.5
E) undefined
A) 3.4
B) 2.0
C) 3.5
D) 5.5
E) undefined
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
23
What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1};
Double max = myList[0];
Int indexOfMax = 0;
For (int i = 1; i < myList.length; i++) {
If (myList[i] >= max) {
Max = myList[i];
IndexOfMax = i;
}
}
System.out.println(indexOfMax);
A) 0
B) 1
C) 2
D) 3
E) 4
Double max = myList[0];
Int indexOfMax = 0;
For (int i = 1; i < myList.length; i++) {
If (myList[i] >= max) {
Max = myList[i];
IndexOfMax = i;
}
}
System.out.println(indexOfMax);
A) 0
B) 1
C) 2
D) 3
E) 4
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck
24
In the following code, what is the printout for list2? class Test {
Public static void main(String[] args) {
Int[] list1 = {1, 2, 3};
Int[] list2 = {1, 2, 3};
List2 = list1;
List1[0] = 0; list1[1] = 1; list2[2] = 2;
For (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + " ");
}
}
A) 1 2 3
B) 1 1 1
C) 0 1 2
D) 0 1 3
Public static void main(String[] args) {
Int[] list1 = {1, 2, 3};
Int[] list2 = {1, 2, 3};
List2 = list1;
List1[0] = 0; list1[1] = 1; list2[2] = 2;
For (int i = 0; i < list2.length; i++)
System.out.print(list2[i] + " ");
}
}
A) 1 2 3
B) 1 1 1
C) 0 1 2
D) 0 1 3
Unlock Deck
Unlock for access to all 25 flashcards in this deck.
Unlock Deck
k this deck