CS180

¡Supera tus tareas y exámenes ahora con Quizwiz!

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:(i-2)

25

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); }

3.0 7.0

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

3.8.0

What is the output of the following code segment?

31 29 29 31 28 28

Consider the following loop, where n is some positive integer for (int i = n; i > 0; i = i / 2) { System.out.print("*");

4

Which of the following statements are true about the following code segment?

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("*");}

5

What is the output of the following code? public class Test { public static void main(String[] args) { int c = 5; a(c); System.out.println(c); } public static void a(int b) { b++; } }

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

How many times will the following program print "Hello World"? int count = 0; while (++count < 10) { System.out.println("Hello World"); }

9

What's the output of the following code segment? int index = 0; while (index < 5) { if (index % 2 == 0) break; System.out.println(index); index++; }

No infinite loop and no output.

Which of the following class is used for file output?

PrintWriter

What will be the output from the following code segment? int num = 0; while (num < 10) { num = num + 1; } System.out.println(num)

10

public class Test { public static void main(String[] args) { a(10); } public static void a(double b) { b++; System.out.print(b); } }

11.0

Given file "nums.txt" with the following content: 1 2 3 4 5 6 what's the output from the following code snippet? Scanner input = new Scanner(new File("nums.txt")); while(input.hasNext()) { System.out.print(input.nextInt());

123456

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 code? int cnt = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { cnt++; } } System.out.println(cnt);

15

What's the output of this Java code when 19 is entered?

19 20

What's the output of the following code segment? int num = 21; for (int i = 2; i <= Math.sqrt(num); i++) { System.out.println(i); if (num % i == 0) { break; } }

2 3

What will be printed after the execution of the following code segment:(i+2)

20

public static void main(String[] args) { Scanner stdin = new Scanner(System.in); double largest = 0; while(stdin.hasNextDouble()) { double input = stdin.nextDouble(); if(input > largest) largest = input; } System.out.println("Largest is: " + largest); }

double largest = 0; is incorrect. We need to initialize largest with the smallest possible value.

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 int 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

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();

***** ***** ***** ***** ***** rectangular

What's the output of the following code snippet? for(int i = 0; i < 5; i++) { System.out.print(i + " "); } int i = 5; System.out.print(i);

0 1 2 3 4 5

What's the output of the following code snippet? int i; for(i = 0; i < 5; i++) { System.out.print(i + " "); } System.out.print(i);

0 1 2 3 4 5

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?

0, str.length() - 1

How may stars will be printed after the execution of the following code segment?

1

How many times will the following program print "Hello World"? int count = 0; do { System.out.println("Hello World"); } while (++count < 10);

10

How may stars will be printed by the following code segment?

10

Which keywords may be used in a switch statement?

ALL

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); } }

Because the two methods have the same signature, the program has a compile error.

What's the output of the following code segment? int index = 0; while (index < 5) { if (index % 2 == 0) continue; System.out.println(index); index++; }

Infinite loop.

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; } }

It compiles fine.

What is the value of str2 after executing the following statements? str1 = stdin.nextLine();str2 = stdin.nextLine();str3 = stdin.nextLine();

Java is powerful

Which of the following statement correctly initializes a Scanner object for reading from a file "info.txt"?

Scanner input = new Scanner(new File("info.txt"));

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.

For the following code segment

The program will end in an infinite loop.

What's the output of the following code snippet? int i = 0; for(int i = 0; i < 5; i++) { System.out.print(i + " "); } System.out.print(i);

There is a compile error.

Read the following program, and rank the scope of the variables from small to large. public class Test { public static final int ZERO = 0; public static void main(String[] args) { test(5); } public static void test(int num) { for (int cnt = num; cnt > ZERO; cnt--) { System.out.println(cnt); } System.out.println("There are " + num + " interations."); } }

ZERO large num in between cnt small

For the following code segment what is the output when the value of m is 1?

a

Given the following input John Smith 323 Washington Ave What is the value of address after executing the following statements?

an empty string

For the following code segment what is the put when the value of m is 2?

bc

For the following code segment what is the put when the value of m is 3?

c

Which of the following are valid types for the expression in a switch statement? Check all that apply.

char string int

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.

cnt=0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ch) { cnt++;

Which of the following loop will execute the loop body at least one time? Check all that apply.

do-while

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 int 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

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 int 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

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

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 loops are pretest loops?

for while

Which of the loops will print out 5 stars?

for (int i = 0; i < 5; i++) { System.out.print("*"); } for (int i = 1; i <= 5; i++) { System.out.print("*"); }

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 is defined as the largest positive integer that can wholly divide m and n.

for (int i = 1; i <= n && i <= m; i++) { if (n % i == 0 && m % i == 0) { gcd = i; } }

Write a for loop to print out the positive odd numbers that are smaller than 10, one number per line.

for(int i = 1;i<=10;i+=2){ System.out.println(i); }

Write a for loop to print out the numbers 1 to n, one number per line.

for(int i = 1;i<=n;i++){ System.out.println(i); }

Complete the following program using for loop to print out all positive even numbers under 30, each number on a separate line.

for(int i = 2;i<=28;i+=2){ System.out.println(i); }

To read from a file using a Scanner, which of the following import statements are needed?

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException;

What's the output of the following code segment? int sum = 0; int index = 0; while (index < 5) { index++; sum += index; if (sum > 5) break; } System.out.println("index: " + index); System.out.println("sum: " + sum);

index: 3 sum: 6

What's the output of the following code segment? int sum = 0; int index = 0; do { index++; sum += index; if (sum > index * 2) break; } while (index < 5); System.out.println("index: " + index); System.out.println("sum: " + sum);

index: 4 sum: 10

Which of the following counts the number of characters 'a' and 'A' in str correctly? 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++; }

Which of the following method from the String class returns the n-th character from String variable str?

str.charAt(n)

Which of the following expressions are true if str1 contains the same sequence of characters as str2?

str1.equals(str2) str1.compareTo(str2) == 0

What is the value of str2 after executing the following statements? str1 = stdin.next();str2 = stdin.next();str3 = stdin.next();

is

What is the value of str2 after executing the following statements? str1 = stdin.next(); str2 = stdin.nextLine(); str3 = stdin.next();

is fun

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

What does a method signature include?

method name The list of the parameter types

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

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.

num=0; System.out.print(num); num=num+2; while(num<=10)

Identify what's wrong with the following function, which computes the sum of all positive factors. // num is a positive integer static int sumOfFactors(int num) { int sum; for(int i = 2; i < num; i++) { if (num % i == 0) sum += i; } return sum;

sum was not initialized. the loop variable i should start with 1 the loop variable i should end with num

Use switch statement to convert a numeric day to the day of the week. The output should be based on the following table.

switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("invalid"); break; }

Given an int variable grade that has been declared and initialized, determine the letter grade and assign it to a string variable letterGrade(declared). In your solution, you should not use any if or if-else statement. Consider using the int division(/) operator.

switch(grade / 10) { case 10: case 9: letterGrade = "A"; break; case 8: letterGrade = "B"; break; case 7: letterGrade = "C"; break; case 6: letterGrade = "D"; break; default: letterGrade = "F"; }

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

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.

while (num > 0) { System.out.print(num + " "); num -= 5; }

Will the program cause a compiler error?

yes


Conjuntos de estudio relacionados

1.13 Unit Test || Graphs of Sinusoidal Functions || Part 1

View Set

US Government AP All Unit Exams (Ortoleva)

View Set

Lesson 9 Water-Soluble Vitamins Chapters 10

View Set

When Friendship Followed Me Home

View Set

The Client with Inflammatory Bowel Disease

View Set

major groups of animals that commonly occur as fossils

View Set

Chapter 11: Solutions- Properties and Behavior

View Set