Computer Science Exam 2
How many times will the following program print "Hello World"? int count = 0; while(++count < 10) System.out.println("Hello World"); }
9
How many lines of output will the following statement display? for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) System.out.println(i * j);
25
What will be printed after the execution of the following code segment? int sum = 0; for(int i = 9; i > 0; i = i - 2) { sum = sum + i; } System.out.print(sum); 25 45 10 30
25
An array is declared as follows: double[] nums = {1.2, 3.5, 5.1, 6.4}; The value of nums[3] is 3.5 1.2 will raise an ArrayIndexOutofBondsException 6.4
6.4
Given the following input John Smith 323 Washington Ave What is the value of address after executing the following statements? firstName = stdin.next(); lastName = stdin.next(); address = stdin.nextLine(); 323 Washington Ave An empty string
An empty string
How many lines of output will the following statement display? for(int i = 0; i < 5; i++) for(int j = 0; j <= i; j++) System.out.println(i * j);
15
What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6}; for(int i = nums.length - 3; i >= 0; i--) { nums[i + 2] = nums[i]; } for(int i: nums) { System.out.print(i + " "); } } }
1 2 1 2 3 4
What is the output of the following code? int cnt = 0; for(int i = 0; i < 5; i++) { for(int j = 0; j < 5 - 1; j++) { cnt++; } } System.out.println(cnt);
15
What's the output of the following code? public static void main(String[] args) { int[] a1 = {1, 2, 3}; int[] a2 = {3, 2, 1}; a1 = a2; a2[0] = 1; a2[1] = 2; a2[2] = 3; for(int i = 0; i < a1.length; i++) System.out.print(a1[i] + " "); }
1 2 3
How many stars will be printed by the following code segment? for(int i = 0; i < 10; i++) System.out.println("*"); compile error 9 10 infinite loop
10
How many times will the following program print "Hello World"? int count = 0; do { System.out.println("Hello World"); } while(++count < 10);
10
Which of the following can be an index of the array double[] nums = new double[50]? An integer variable i, which has been declared and initialized with value 0. (int) (Math.random() * 50) i + 50 i Math.random() * 50
(int) (Math.random() * 50) i
What will be displayed by the call print("*", 4) based on the following implementation? public static void print(String symbol, int num) { while(num > 0) { System.out.println(symbol); num--; } }
* * * *
What is the output of the following code? for(int i = 0; i 5; i++) { for(int j = 0; j < 5; j++) { System.out.print("*"); } System.out.println(); }
***** ***** ***** ***** *****
What's the output of the following code? public static void main(String[] args) { int[] vals = {4, 3, 2, 1}; int index; for(int v ; vals) { index = -1; for(int i = 0; i < vals.length; i++) { if(vals[i] == v) index = i; } System.out.println(index); } }
0 1 2 3
What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = new int[5]; int i; for(i = 0; i < nums.length; i++) { nums[i] = i; System.out.print(nums[i] + " "); } } }
0 1 2 3 4
What's the output of the following loop? for(int count = 0; count <= 5; count++) { System.out.print(count + "*"); }
0* 1* 2* 3* 4* 5*
For a String str, what are the indexes of the first and last characters? 1, str.length() 1, str.length() - 1 0, str.length() - 1 0, str.length()
0, str.length() - 1
How many stars will be printed after the execution of the following code segment? for(int i = 0; i < 10; i++); System.out.println("*"); compile error 1 10 infinite loop
1
What will be the output from the following code segment? int num = 0; while(num < 10) { num = num + 1; } System.out.println(num); 9 11 10 cannot determine
10
What's the output of the following code? public static void print(String symbol, int count) { while(count > 0) { System.out.println(symbol); count--; } } public static void main(String[] args) { int n = 3; printStars("*", n); System.out.print(n); }
3
What's the output of the following code segment if the following is used as the input from the keyboard? 5 3 6 5 7 8 2 1 9 0 public static void main(String[] args) { Scanner stdin = new Scanner(System.in); double target = stdin.nextDouble(); int cnt = 0; double val = 1; while(val > 0 && cnt < 3) { val = stdin.nextDouble(); if(val > target) { cnt++; } } System.out.print(snt + " " + val); stdin.close(); }
3 8.0
What's the value of x and y after the call of swap(x, y) as shown in the following code segment? public static void swap(double x, double y) { double temp = x; x = y; y = temp; } public static void main(String[] args) { double x = 3.0, y = 7.0; swap(x, y); } x: y:
3.0 7.0
How many stars will the above code produce when n = 15? 4 15 5 7
4
Based on the implementation of the following methods public static int log(int num) { int res = 0; while(num > 0) { num = num / 2; res++ } return res; } public static int pow(int base, int exp) { int res = 1; while(exp-- > 0) { res *= base; } return res; } log(10): pow(2, log(10)):
4 16
What's the output of the following code? public static void main(String[] args) { int[] a1 = {1, 2, 3}; int[] a2 = {3, 2, 1}; a1 = a2; a2[0] = 4; a2[1] = 5; a2[2] = 6; for(int i = 0; i < a1.length; i++) System.out.print(a1[i] + " "); }
4 5 6
Which of the following statements are true about the following code segment? int sum = 0; for(int i = 0; i < 10;) { sum = sum + i; i++; } System.out.println(sum); The program will end in an infinite loop 55 will be printed The program has a complie error 45 will be printed
45 will be printed
How many lines of stars will be printed by the following code segment? int num = 0; while(num++ < 5) { System.out.println("*"); } 6 5 4 7
5
What's the output of the following code? int num = 17; int cnt = 0; while(num != 0) { num = num / 2; cnt++; } System.out.print(cnt);
5
What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = { 2, 4, 6, 6, 5, 6 }; int max = nums[0]; int indexOfMax = 0; for(int i = 1; i < nums.length; i++) { if(nums[i] > max) { max = nums[i]; indexOfMax = i; } } System.out.println(max + "," + indexOfMax); } }
6, 2
boolean repeats = false; String str = "abaab"; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == str.charAt(i + 1)) repeats = true; } System.out.print(repeats); Which of the following statements is true? it displays true An exception will occur on line 4 it displays false
An exception will occur on the line 4
What is the output of the following code? public class Test { public static void main(String[] args) { m(2); } public static void m(double a) { System.out.println(a); } public static void m(double b) { System.out.println(a*2); } } 2 Because the two methods have the same signature, the program has a compile error 2 2 The program will not run because none of the two methods can be invoked by having an int parameter
Because the two methods have the same signature, the program has a compile error
Analyze the following main method: public static void main(String[] args) { int num = (int) (Math.random() * 2); if (num % 2 == 0) { System.out.println("head"); } else { System.out.println("tail"); return; } } There is a compile error because we need to add a return after line 4 It compiles fine There is a compile error because there should not be a return in the main method
It compiles fine
What is the output of the following program? public class Test { public static void main(String[] args) { String[] strs = {"Hello", "World"}; for(String str; strs) { System.out.print(str + "#"); } } }
Hello#World#
Given the following input Coding is fun Java is powerful Practice is important What is the value of str2 after executing the following statements? str1 = stdin.nextLine(); str2 = stdin.nextLine(); str3 = stdin.nextLine(); A blank space Java is powerful Practice is important is
Java is powerful
For the following two methods, which one will be invoked by the statement double z = m(5, 6.4);? Method 1: int m(double x, double y) Method 2: int m(int x, double y) Method 3: int m(int x, int y)
Method 2
Which of the following is an equivalent implementation of the following code snippet? String longest = ""; for(int i = 0; i < names.length; i++) { if(longest.length() < names[i].length()) { longest = names[i]; } }
String longest = ""; for(String s ; names) { if(longest.length() < s.length()) { longest = s; } }
Which of the following statements are valid Java statements? String strings[] = {"a", "b", "c"}; int nums[] = {1, 2, 2}; int[] num = {1, 2, 3}; char[] chars = {a, b, c};
String strings[] = {"a", "b", "c"}; int nums[] = {1, 2, 2}; int[] num = {1, 2, 3};
Complete the following program so that the program prints out: 0 2 4 6 8 10 There is no space at the beginning or the end of the output, and there is a space between two numbers. public class Test { public static void main(String[] args) { int num; // your code starts here // your code ends here { System.out.print(" "); System.out.print(num); num = num + 2; } } }
System.out.print(0); num = 2; while(num <= 10)
Given a String str and a char ch, which have been declared and initialized, write a segment of code to determine how many times ch appears in str and store the result to an int variable cnt(declared). To run the program, enter the string and the character on two separate lines. Make sure there are no blank lines. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String str = stdin.nextLine(); char ch = stdin.nextLine().charAt(0); int cnt = 0; // your code starts here // your code ends here System.out.println(cnt); stdin.close(); }
cnt = 0; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == ch) { cnt++; } }
Consider the following code: String name = "cs180"; name.toUpperCase(); System.out.print(name); What's the output? CS cs180 CS180
cs180
Which of the following loops will execute the loop body at least one time?
do-while
What does a method signature include? The list of the parameter types List of parameter names return type method name
The list of the parameter types method name
Consider the following program: public class Print5 { public static void main(String[] args) { print5(); } public static void print5() { for(int i = 0; i < 5; i++) System.out.print("*"); } } The program outputs nothing The program outputs five stars There is a compile error because the method print5() appears after main There is a compile error because there is no parameter in method print5()
The program outputs five stars
Consider the following program: public class Print5 { public static void main(String[] args) { // print5(); } public static void print5() { for(int i = 0; i < 5; i++) System.out.print("*"); } } The program outputs nothing The program outputs five stars There is a compile error because the method print5() appears after main There is a compile error because there is no parameter in method print5()
The program outputs nothing
Which description about the following code segment is correct? int discount = 0; for(long days = 0; days < 10;) { discount += 5; } System.out.println(discount + "%"); The program runs in a infinite loop because the control condition will always be true. The program has a compiler error because the control variable in the for loop cannot be the type of long. The program compiles and runs fine. The output of the program is 50%.
The program runs in a infinite loop because the control condition will always be true
What is the output of the following program? public class Test { public static void main(String[] args) { int[] nums = new int[5]; int i; for(i = 0; i < nums.length; i++) { nums[i] = i; } System.out.print(nums[i] + " "); } } }
The program will cause ArrayIndexOutOfBoundsException
For the following code segment int sum = 0; for(int i = 0; i < 5;) { sum = sum + i; } System.out.print(sum); 10 will be printed The program will end in an infinite loop 15 will be printed The program has a compile error
The program will end in an infinite loop
What's the output of the following program? public class SignatureDemo { public static double max(double x, double y) { System.out.println("double double"); if(x > y) return x; else return y; } public static double max(double x, int y) { System.out.println("double int"); if(x > y) return x; else return y; } public static double max(int m, int n) { System.out.println("int int"); if(m > n) return m; else return n; } public static void main(String[] args) { System.out.println(max(3.7, 5.4)); } }
double double 5.4
What's the output of the following program? public class SignatureDemo { public static double max(double x, double y) { System.out.println("double double"); if(x > y) return x; else return y; } public static double max(double x, int y) { System.out.println("double int"); if(x > y) return x; else return y; } public static double max(int m, int n) { System.out.println("int int"); if(m > n) return m; else return n; } public static void main(String[] args) { System.out.println(max(3, 7.5)); } }
double double 7.5
Which of the statements are valid? String[] str = new String(); int num[] = new double[30]; double f[] = {3.4, 5.8, 6.9, 7.2}; double[] d = new double[20];
double f[] = {3.4, 5.8, 6.9, 7.2}; double[] d = new double[20];
What's the output of the following program? public class SignatureDemo { public static double max(double x, double y) { System.out.println("double double"); if(x > y) return x; else return y; } public static double max(double x, int y) { System.out.println("double int"); if(x > y) return x; else return y; } public static double max(int m, int n) { System.out.println("int int"); if(m > n) return m; else return n; } public static void main(String[] args) { System.out.println(max(3.5, 7)); } }
double int 7.0
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int size = stdin.nextInt(); double[] vals = new double[size]; for(int i = 0; i < size; i++) { vals[i] = stdin.nextDouble(); } double stdDeviation; // your code starts here // your ends here System.out.println(stdDeviation); } }
double mean = 0; for(int i = 0; i < size; i++) { double val = vals[i]; mean += val; } mean /= size; stdDeviation = 0; for(int i = 0; i < size; i++) { double val = vals[i]; stdDeviation += Math.pow(val - mean, 2); } stdDeviation /= size; stdDeviation = Math.sqrt(stdDeviation);
Complete the following program, which reads three numbers from input and outputs the maximum among the three numbers. The implementation already has a method double maximum(double n1, double n2, double n3). import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); // your code starts here // tour code ends here System.out.print(max); stdin.close(); } public static double maximum(double n1, double n2, double n3,) { double res = n1; if(n2 > res) res = n2; if(n3 > res) res = n3; return res; } }
double n1 = stdin.nextDouble(); double n2 = stdin.nextDouble(); double n3 = stdin.nextDouble(); double max = Integer.MAX_VALUE; if(n1 >= n2 && n1 >= n3) { max = n1; } else { if(n2 >= n3) { max = n2; } else { max = n3; } }
Declare a double array named nums with 20 elements. Initialize each element in the array with the square root of its index. public class Test { public static void main(String[] args) { // your code starts here // your code ends here for(int i = 0; i < nums.length, i++) { System.out.println(nums[i]); } } }
double[] nums = new double[20]; for(int i = 0; i < nums.length; i++) { nums[i] = Math.sqrt(i); }
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int num; num = stdin.nextInt(); int[] vals = new int[num]; for(int i = 0; i < num; i++) { vals[i] = stdin.nextInt(); } boolean exist; // your code starts here // your code ends here System.out.println(exist); } }
exist = false; int sum = vals[0] + vals[1]; for(int i = 2; i < vals.length; i++) { sum = sum + vals[i]; if(sum == 20) exist = true; sum = sum - vals[i - 2]; }
Implement the method existConsecutive(int[] vals) , which returns true if there are duplicate values next to each other. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int num; num = stdin.nextInt(); int[] vals = new int[num]; for(int i = 0; i < num; i++) { vals[i] = stdin.nextInt(); } boolean existConsecutive; // your code starts here // your code ends here System.out.println(existConsecutive); } }
existConsecutive = false; for(int i - 0; i < vals.length - 1; i++) { if(vals[i] == vals[i + 1]) existConsecutive = true; }
True or false: All the parameters in a method must be of the same type
false
True or false: Every method must have a return statement
false
True or false: If a method test() has a return value, the following statement will cause a compile error. test();
false
Which of the following loop are pretest loops?
for while
The following program takes a positive integer n and displays a triangle of stars with n lines. Use the sample runs to get the specification of the triangle. import java.util.Scanner; public class Test { public static void main(String[] args) { scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here stdin.close(); } }
for(int count = 0; count < n; count++) { for(int i = 0; i <= count; i++) { System.out.print("*"); } System.out.println(); }
Which of the loops will print out 5 stars? for(int i = 1; i < 5; i++) { System.out.print("*"); } for(int i = 0; i <= 5; i++) { System.out.print("*"); } for(int i = 0; i < 5; i++) { System.out.print("*"); } for(int i = 1; i <= 5; i++) { System.out.print("*"); }
for(int i = 0; i < 5; i++) { System.out.print("*"); } for(int i = 1; i <= 5; i++) { System.out.print("*"); }
The following program takes a positive integer n and displays a triangle of stars with n lines. Use the sample runs to get the specification of the triange. import java.util.Scanner; public class Test { public static void main(String[] args) { scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here stdin.close(); } }
for(int i = 0; i < n; i++) { for(int j = n - 1; j > i; j--) { System.out.print(" "); } for(int j = 0; j < (2 * i + 1); j++) { System.out.print("*"); } System.out.println(); }
Complete the following code segment to print a rectangle based on input m and n, where m is the width and n is the height. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int m = stdin.nextInt(); int n = stdin.nextInt(); // your code starts here // your code ends here } public static void print(String str, int num) { for(int i = 0; i < num; i++) System.out.print(str); } }
for(int i = 0; i < n; i++) { print("*", m); System.out.println(); }
Complete the following code segment to print a diamond based on input n. import java.util.Scanner; public class Test { public static void main(String[] args) { scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here } public static void print(String str, int num) { for(int i = 0; i < num; i++) System.out.print(str); } }
for(int i = 0; i < num; i++) { print(" ", num - 1 - i); System.out.print("*"); print(" ", 2 * i - 1); if(i > 0) System.out.print("*"); System.out.println(); } for(int i = num - 2; i >= 0; i--) { print(" ", num - 1 - i); System.out.print("*"); print(" ", 2 * i - 1); if(i > 0) System.out.print("*"); System.out.println(); }
import java.util.Scanner; public class Test { public static void main(String[] args) { scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here } public static void print(String str, int num) { for(int i = 0; i < num; i++) System.out.print(str); } }
for(int i = 0; i < num; i++) { print(" ", num - 1 - i); print(" *", 2 * i + 1); System.out.println(); } for(int i = num - 2; i >= 0; i--) { print(" ", num - 1 - i); print("*", 2 * i + 1); System.out.println(); }
Write a for loop to print out the positive odd numbers that are smaller than 10, one number per line. public class Test { public static void main(String[] args) { // your code starts here // your code ends here } }
for(int i = 1; i < 10; i = i + 2) System.out.println(i);
Complete the following Java program to print out the multiplication table from 1 to 9. public class Test { public static void main(String[] args) { // your code starts here // your code ends here } }
for(int i = 1; i <= 9; i++) { for(int j = 1; j < i; j++) { System.out.print(j * i + " "): } System.out.println(i * i); }
Write a for loop to print out the numbers 1 to n, one number per line. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here stdin.close(); } }
for(int i = 1; i <= n; i++) { System.out.println(i); }
Complete the following code segment to print a rectangle based on input m and n, where m is the width and n is the height. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int m = stdin.nextInt(); int n = stdin.nextInt(); // your code starts here // your code ends here } public static void print(String str, int num) { for(int i = 0; i < num; i++) System.out.print(str); } }
for(int i = 1; i <= n; i++) { if(i == 1 || i == n) print("*", m); else { System.out.print("*"); print(" ", m - 2); System.out.print("*"); } System.out.println(); }
Complete the following program using for loop to print out all positive even numbers under n, each number on a separate line. n is an integer from the user input. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); // your code starts here // your code ends here stdin.close(); } }
for(int i = 2; i < n; i = i + 2) { System.out.println(i); }
Given two positive integers m and n, compute and display the greatest common divisor of m and n. The greatest common divisor of m and n. The greatest common divisor of m and n is defined as the largest positive integer that can wholly divide m and n. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int m = stdin.nextInt(); int n = stdin.nextInt(); int gcd = 1; // your code starts here // your code ends here System.out.print(gcd); stdin.close(); } }
for(int i = 2; i <= m && i <= n; i++) { if(m % i == 0 && n % i == 0) gcd = i; }
Convert the following program into an equivalent using a do-while loop. public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int num = stdin.nextInt(); while(num > 0) { System.out.println(num); num = num / 2; } stdin.close() } } import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int num = stdin.nextInt(); // your code starts here // your code ends here stdin.close(); } }
if(num > 0) { do { System.out.println(num); num = num / 2; } while(num > 0); }
Which of the following counts the number of characters 'a' and 'A' in str? Check all that apply. int cnt = 0; for(int i = str.length() - 1; i >= 0; i--) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } int cnt = 0; for(int i = 0; i < str.length; i++) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } int cnt = 0; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } int cnt; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') snt++; }
int cnt = 0; for(int i = str.length() - 1; i >= 0; i--) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; } int cnt = 0; for(int i = 0; i < str.length(); i++) { if(str.charAt(i) == 'a' || str.charAt(i) == 'A') cnt++; }
What's the output of the following program? public class SignatureDemo { public static double max(double x, double y) { System.out.println("double double"); if(x > y) return x; else return y; } public static double max(double x, int y) { System.out.println("double int"); if(x > y) return x; else return y; } public static double max(int m, int n) { System.out.println("int int"); if(m > n) return m; else return n; } public static void main(String[] args) { System.out.println(max(3, 5)); } }
int int 5
The following program reads a sequence of integers from the keyboard, and displays the sum of the positive odd integers from the input. The sequence ends with a 0. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int sumOfPositiveOdd = 0; // your code starts here // your code ends here System.out.print(sumOfPositiveOdd); stdin.close(); } }
int num; do { num = stdin.nextInt(); if(num > 0 && num % 2 == 1) sumOfPositiveOdd += num; } while(num != 0);
Declare an int array variable names nums and allocate an array that holds 20 integer value to it. public class Test { public static void main(String[] args) { // your code starts here // your code ends here nums[0] = 0; nums[5] = 5 * 5; nums[10] = 10 * 10; nums[19] = 19 * 19; System.out.println(nums[0]); System.out.println(nums[5]); System.out.println(nums[10]); System.out.println(nums[19]); System.out.println(nums.length); } }
int[] nums = new int[20];
Declare an integer array nums and initialize it with 1 through 20. public class Test { public static void main(String[] args) { // your code starts here // your code ends here for(int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } } }
int[] nums = new int[20]; for(int i = 0; i < nums.length; i++) { nums[i] = i + 1; }
Given the following input Coding is fun Java is powerful Practice is important What is the value of str2 after executing the following statements? str1 = stdin.next(); str2 = stdin.next(); str3 = stdin.next(); Java is powerful is A blank space Practice is important
is
Given the following input Coding is fun Java is powerful Practice is important What is the value of str2 after executing the following statements? str1 = stdin.next(); str2 = stdin.nextLine(); str3 = stdin.next(); is is fun is fun Empty string
is fun
boolean repeats = false; String str = "abaab"; for(int i = 0; !repeats && i < str.length(); i++) { if(str.charAt(i) == str.charAt(i + 1)) repeats = true; } System.out.print(repeats); Which of the following statements is true? it displays true An exception will occur on line 4 it displays false
it displays true
What is the output of the following code? int num = 0; while(num < 4) { num = num + 1; System.out.println("num is " + num); }
num is 1 num is 2 num is 3 num is 4
What is the expression for the second element an array nums?
nums[1]
Given a user input integer num, use a while loop to determine whether or not num is a prime number or not, If yes, assign true to a boolean variable prim(declared already), otherwise, assign false to prime.
prime = true; int cnt = 2; while(cnt < num) { if(num % cnt == 0) { prime = false; break; } cnt++; }
Complete the implementation of the following program by adding the proper method header. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int year = stdin.nextInt(); System.out.println(isLeap(year)); stdin.close(); } // your code starts here // your code ends here { return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0; } }
public static boolean isLeap(int year)
Which of the following methods from the String class returns the n-th character from String variable str? str.at(n) str.char(n) str.str(n) str.charAt(n)
str.charAt(n)
Which of the following expressions are true if str1 contains the same sequence of characters as str2? str1.equals(str2) str1 = str2 str1 == str2 str1.compareTo(str2) == 0
str1.equals(str2) str1.compareTo(str2) == 0
Which of the following gives the length of an array strs?
strs.length
True or false: If a method test() does not have a return value, it should not appear in the following statement. x = 5 * test();
true
Suppose your method does not return any value, which of the following can be used as the return type?
void
Given a String str which has been declared and initialized, write a segment of code to reverse str and store the result to variable reversed(declared). To run, enter the test strings in the input. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String str = stdin.nextLine(); String reversed; // your code starts // your code ends System.out.println(reversed); stdin.close(); } }
reversed = ""; for(int i = 0; i < str.length(); i++) { reversed = str.charAt(i) + reversed; }
What's the output of the following code segment? System.out.println("start"); int count = 1; do { System.out.println("count is " + count); count++ } while(count < 3); System.out.println("end");
start count is 1 count is 2 end
Complete the following code segment that will print out the following result using a while statement. 20 15 10 5 0 There is a space between two numbers and no space or newline after the last number. public class Test { public static void main(String[] args) { int num = 20; // your code starts here // your code ends here System.out.print(0); } }
while(num > 0) { System.out.print(num + " "); num -= 5; }