COP2210 All questions for FINAL review

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

What does the code fragment print? int checkNumber=201; System.out.print(checkNumber++ + ", "); System.out.println(checkNumber);

201, 202

Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet? Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String num1 = in.nextLine(); System.out.print("Enter another number: "); String num2 = in.nextLine(); System.out.println(num1 + num2);

2345

What is the output of this code snippet? int sum = 22; sum = sum + 2; System.out.print(sum); // sum = sum + 4; System.out.print(sum);

2424

What does the code fragment print? int i=250; System.out.print(i--); System.out.println(", " + ++i);

250, 250

Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.nextLine(); System.out.print("Enter another number: "); String n2 = in.nextLine(); String result = n1 + n2; System.out.print(result);

4562

If the user entered the sequence of numbers 2 7 1 8 6 3, what would be the value printed by the System.out.println(m) statement given at the end of the program? Scanner in = new Scanner(System.in); int m=7, n; for(int j=0; j<6; j++) { System.out.print("Enter a number: "); n = in.nextInt(); if (n > m) m=n; } System.out.println(m);

8

How many times does the code snippet given below display "Loop Execution"? int i = 1; while (i != 10) { System.out.println("Loop Execution"); i++; }

9 times

What is the value of Math.pow(3, 2)?

9.0

Assuming that a user enters 64 as the score, what is the output of the following code snippet? int score = 0; Scanner in = new Scanner(System.in); System.out.print("Enter your score: "); score = in.nextInt(); if (score < 40) { System.out.println("F"); } else if (score >= 40 || score < 50) { System.out.println("D"); } else if (score >= 50 || score < 60) { System.out.println("C"); } else if (score >= 60 || score < 70) { System.out.println("B"); } else if (score >= 70 || score < 80) { System.out.println("B+"); } else { System.out.println("A"); }

A

Which statement best describes a computer program?

A program is a sequence of instructions and decisions that the computer carries out.

Suppose a user enters the character B. What would be the output? char c = in.nextLine().charAt(0); switch (c) { case 'B': System.out.print("B "); case 'C': System.out.print("C "); case 'D': System.out.print("D "); case 'E': System.out.print("E "); break; default: System.out.print("F "); }

B C D E

Which of the following variables is used to store a condition that can be either true or false?

Boolean

What will be the value printed? for(int i=0; i<20; ++i) { --i; } System.out.println(i);

None; the program contains a compilation error

Assuming that a user enters 56 for age, what is the output of the following code snippet? int age = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter your age: "); age = in.nextInt(); if (age < 13) { System.out.println("Kid!"); } if (age >= 13 && age < 19) { System.out.println("Teen!"); } if (age >= 19 && age < 30) { System.out.println("Young!"); } if (age >= 30 && age < 50) { System.out.println("Adult!"); } if (age >= 50) { System.out.println("Old!"); }

Old!

What kind of operator is the <= operator?

Relational

What (if any) type of error occurs with the following code if the user input is ABC? Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.nextLine(); int count = Integer.parseInt(str); System.out.println("Input is " + count);

Run-time error

The error message "cannot find symbol" is usually a good clue that what kind of error has been made?

Spelling

What is the name of the type that denotes a string of characters?

String

Which class is part of the java.lang package?

String

Which of the following declares a variable that will store a welcome message?

String welcome;

Which one of the following statements displays the output as 54321.00?

System.out.printf("%8.2f", 54321.0);

What is the syntax for calling the println method on the object System.out?

System.out.println("Any message");

What is wrong with the following code snippet? int average; average = 78A;

The average variable is assigned a non-numeric value.

Why are break statements needed inside a switch's case statement?

The break statements cause the program to jump out of the switch

What is wrong with the following code snippet? int size = 42; int cost = 9.99; System.out.println("size = " + size); System.out.println(" cost = " + cost);

The code snippet attempts to assign a decimal value to an integer variable.

What is the result of the following statement? String s = "You" + "had" + "me" + "at" + "hello";

The string s has the following value: "Youhadmeathello"

What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 11) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum);

The value of sum is 66.

What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 6) { sum = sum + i; i+=2; } System.out.println("The value of sum is " + sum);

The value of sum is 9.

Why is the switch statement below incorrect? double option = 3.0; switch (option) { case 1.0: System.out.println("First choice"); case 2.0: System.out.println("Second choice"); break; case 3.0: System.out.println("Third choice"); break; default: System.out.println("Invlaid choice"); }

The variable "option" is declared as a double variable.

Assuming the programmer wishes to display "Hello!" on the screen, which statement is true about the following Java code fragment: System.out.println("Hello!");

There are no errors.

What is the output of the following code snippet? String str1; str1 = "I LOVE MY COUNTRY"; String str2 = str1.substring(4, 11); System.out.println(str2);

VE MY C

Is the following code snippet legal? boolean b = false; do { System.out.println("Do you think in Java?"); } while (b != b);

Yes, it is legal and prints "Do you think in Java?" once.

Suppose day is a String variable. Is there any logical error in the SWITCH statement below? switch(day.toUpperCase()) { case "Monday": case "Tuesday": case "Wednesday": System.out.println("Store is Open 24 hours"); break; case "Thursday": case "Friday": System.out.println("Store is Open 8 hours"); break; case "Saturday": System.out.println("Store is Open 4 hours"); break; default: System.out.println("Store is Closed"); }

Yes, there is an error: the days of the week listed should be all in capital letters

Which of the following statements about constants is correct?

You can make a variable constant by using the final reserved word when declaring it.

How many times does the following loop execute? for (double d = 1; d != 10; d++) { d = d / 3; System.out.print(d + " "); }

an infinite number of times

What is the name of the = operator in Java?

assignment

In a switch statement, if a break statement is missing

execution falls through the next branch until a break statement is reached

Which of the following loops executes 8 times?

for(i = 0; i <= 7; i++)

What term is used to refer to languages that allow programmers to describe tasks at a higher conceptual level than machine code?

high-level

What is the conditional required to check whether the length of a string s1 is odd?

if ((s1.length() % 2) != 0)

Which is the correct way to compare 2 strings of characters s1 and s2?

if (s1.equals(s2))

Which if statement is true when the length of string s1 is greater than 42?

if (s1.length() > 42)

Which of the following performs the same way as the switch statement below? switch (value) { case 1: System.out.print ("Small"); break; case 10: System.out.print ("Large"); break; default: System.out.print ("Other"); break; }

if (value == 1) System.out.print("Small"); else if (value == 10) System.out.print("Large"); else System.out.print("Other");

To use a class in another package you need to ________ it.

import

What is the output of the following code snippet? int i = 1; while (i <= 10) { System.out.println("Inside the while loop"); i = i + 10; }

"Inside the while loop" will be displayed only once.

Which one of the following operators computes the remainder of an integer division?

%

Which operator is used to concatenate two or more strings?

+

The Java compiler ignores any text between ____.

/* and */

In Java, a comment on a line begins with which characters?

//

What is the output of the following code snippet? int i = 1; while (i != 9) { System.out.print(i + " "); i++; if (i == 9) { System.out.println("End"); } }

1 2 3 4 5 6 7 8 End

What is the output of the following code snippet? int i = 1; while (i < 20) { System.out.print(i + " "); i = i + 2; if (i == 15) { i = 19; } }

1 3 5 7 9 11 13 19

How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; }

10

What is the output of the following code snippet? int counter = 1; for (double i = 0.01; i <= 1.0; i = i + 0.01) { counter++; } System.out.println(counter);

100

What output is produced by these statements? String name = "Joanne Hunt"; System.out.println(name.length());

11

What will be the value of j printed? int j=0; for(; j<10; j +=3); { j--; } System.out.println(j);

11

What is the output of the following code snippet? int num = 100; if (num < 100) { if (num < 50) { num = num - 5; } else { num = num - 10; } } else { if (num > 150) { num = num + 5; } else { num = num + 10; } } System.out.println(num);

110

How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); }

13

What is the output of the following code: int num1 = 6; int num2 = 10; num1 = num1 + num2; num2 = num1 + num2; System.out.println(num1 + ", " + num2);

16, 26

Suppose a user enters the number 2. What would be the output? int number = in.nextInt(); switch (number) { case 1: System.out.print("1 "); case 2: System.out.print("2 "); case 3: System.out.print("3 "); break; case 4: System.out.print("4 "); break; default: System.out.print("Any number "); }

2 3

How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 2 == 0) { j--; } } while (j >= 1);

2 times

What values does counter variable i take on when this loop executes? for (int i = 20; i >= 2; i = i - 6) { System.out.print(i + ","); }

20, 14, 8, 2, -4

The operator !> stands for

this is not an operator in Java

What is the purpose of the assignment operator?

to change the value of a variable

By convention among Java programmers, class names begin with a(n) _____________.

uppercase letter

What is the name of the type that denotes whole numbers?

int

Which of the following options defines an integer variable?

int age;

Which of the following declares a variable that will store a count with an integer value?

int count;

Which of the following code snippets displays the output exactly 10 times?

int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; }

Which of the following statements places input into the variable value given this line of code? Scanner in = new Scanner(System.in);

int value = in.nextInt();

Which package is automatically imported in any Java program?

java.lang

The JOptionPane class is located in the

javax.swing package

What term is used to refer to the computer instructions that are executed by a CPU?

machine code

Which of the following is the best choice for a variable identifier that will store a name?

name

What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0;

num1 = 4.20 and num2 = 47.0

How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); }

only one time

How do you extract the first 5 characters from the string str?

str.substring(0,5)

What is the output of the following code: int num1 = 6; int num2 = num1; num2 = num2 + 10; System.out.println(num1);

6

What is the value of the price variable after the following code snippet is executed? int price = 42; if (price < 40) { price = price + 10; } if (price > 30) { price = price * 2; } if (price < 100) { price = price - 20; }

64

What is the value of the following expression? 2 + 3 + 5 / 2

7

What is the output of the following code snippet? double a; a = Math.sqrt(9.0) + Math.sqrt(16.0); System.out.println(a);

7.0

Assuming that a user enters 50, 70, and 60 as input values one after another, separated by spaces, what is the output of the following code snippet? int number1 = 0; int number2 = 0; int number3 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); number1 = in.nextInt(); System.out.print("Enter a number: "); number2 = in.nextInt(); System.out.print("Enter a number: "); number3 = in.nextInt(); if (number1 > number2) { if (number1 > number3) { System.out.println(number1); } else { System.out.println(number3); } } else { if (number2 > number3) { System.out.println(number2); } else { System.out.println(number3); } }

70

What tool translates high-level instructions into low level machine code?

Compiler

What is the name of the type that denotes floating-point numbers that can have fractional parts?

Double

What is the output of this code fragment? char c = 'A'; switch (c) { case 'B': System.out.print("B "); case 'C': System.out.print("C "); case 'D': System.out.print("D "); case 'E': System.out.print("E "); break; default: System.out.print("F "); }

F

What is the output of the following code snippet? String str = "Java Is Good"; int n = str.length(); String mystery = str.substring(n - 4, n) + str.charAt(4) + str.substring(0, 4); System.out.println(mystery);

Good java

What is the output of this code fragment? int x = 3; switch (x) { case 1: System.out.println("Hi"); break; case 2: System.out.println("Hello"); break; case 3: System.out.println("Good morning"); break; case 4: System.out.println("Welcome"); break; default: System.out.println("Bye"); }

Good morning

What will be the output of the following code snippet? boolean token = false; do { System.out.println("Hello"); } while (token);

Hello will be displayed only once.

What is the name of the file that contains the Java source code for the public class HelloPrinter?

HelloPrinter.java

Which statement causes an immediate exit from a switch?

break

Which is the Java equivalent of the following mathematical expression? c = √(a2 + b2)

c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

Which one of the following statements can be used to get the fifth character from a string str?

char c = str.charAt(4);

What term is used to refer to text in a program that is an explanation for human readers of the code?

comments

What is the result of the following code snippet? double circleRadius; double circleVolume = 22 / 7 * circleRadius * circleRadius; System.out.println(circleVolume);

compile-time error

Which one of the following statements defines a constant with the value 123?

const int MY_CONST = 123;

Which statement stores an integer value in a variable?

count = 5;

Assuming that the variable count has been declared as type int, which statement changes the value of count?

count = 6;

Assume that the variable count has been declared as type int. Which statement adds 10 to count?

count = count + 10;

Assume the following variables have been declared as shown and given values elsewhere: double a, b, c; The discriminant of a quadratic equation is defined to be: Which of the following statements will assign the correct value to discriminant?

double discriminant = Math.sqrt(b * b - 4 * a * c);

Which of the following declares a variable that will store a measurement with fractional parts?

double measure;


Conjuntos de estudio relacionados

Chapter 5 Practice Test Questions: NJ Laws, Rules, and Regulations Common to All Lines

View Set

AD RESEARCH FINAL: Descriptive Statistics

View Set

Saunders C14 Calculation of Medication and Intravenous Prescriptions

View Set

Exam One - Psychological Assessment

View Set