CS 145 Midterm

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

Imagine you are writing a personal fitness program that stores the user's age, gender, height (in feet or meters), and weight (to the nearest pound or kilogram). Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. age gender height weight

int age = 0; String gender = " "; double height = 0.0; int weight = 0;

What is the output produced from the following statements? System.out.println("\"Quotes\""); System.out.println("Slashes \\//"); System.out.println("How '\"confounding' \"\\\" it is!");

"Quotes" Slashes \// How '"confounding' "\" it is!

What is the output of the following loop? System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+");

+----+ \ / / \ \ / / \ \ / / \ +----+

What is the value of variable x after the following code executes? int x = 3; x = x + 2; x = x + x; 3 10 12 5 7

10

What is the result of the following expression? 1 + 2 * 3 + 7 * 2 % 5

11

Which of the following are legal int literals? '7' 10.0 22 -6875309 5. -1 1.5 2.3

22 -6875309 -1

What is the output of the following oddStuff method? public static void oddStuff() { int number = 4; for (int count = 1; count <= number; count++) { System.out.println(number); number = number / 2; } }

4 2

What is the output from the following code? int max; int min = 10; max = 17 - 4 / 10; max = max + 6; min = max - min; System.out.println(max * 2); System.out.println(max + min); System.out.println(max); System.out.println(min);

46 36 23 13

Trace the evaluation of the following expressions, and give their resulting values. Make sure to give a value of the appropriate type (such as including a .0 at the end of a double). 2 + 3 * 4 - 6 (238 % 10 + 3) % 7 (18 - 7) * (43 % 10)

8 4 33

Which of the following can be used in a Java program as identifiers? Check all of the identifiers that are legal. ABC "hello" _average AnnualSalary for sum_of_data first-name println B4 42isThesolution

ABC _average AnnualSalary sum_of_data println B4

What is the difference between the file MyProgram.java and the file MyProgram.class?

MyProgram.java is a source code file typed by the programmer, and MyProgram.class is a compiled executable class file that is run by the computer.

What is the output produced from the following statements? System.out.println("Shaq is 7'1"); System.out.println("The string \"\" is an empty message."); System.out.println("\\'\"\"");

Shaq is 7'1 The string "" is an empty message. \'""

Imagine you are writing a program that stores a student's year (Freshman, Sophomore, Junior, or Senior), the number of courses the student is taking, and his or her GPA on a 4.0 scale. Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. year courses taken GPA

String year = " "; int courses = 0; double gpa = 0.0;

What series of println statements would produce the following output? This is a test of your knowledge of "quotes" used in 'string literals.' You're bound to "get it right" if you read the section on ''quotes.''

System.out.print("This is a test of your\nknowledge of \"quotes\" used\nin 'string literals.'\n\nYou're bound to \"get it right\"\nif you read the section on\n' 'quotes.' '");

Write a println statement that produces the following output: / \ // \\ /// \\\

System.out.println(" / \\ // \\\\ /// \\\\\\ ");

Which of the following is the correct syntax to output a message? System.println(Hello, world!); System.out.println("Hello, world!"); Out.system.println"(Hello, world!)"; System.println("Hello, world!"); System.println.out('Hello, world!');

System.out.println("Hello, world!");

What series of println statements would produce the following output? "Several slashes are sometimes seen," said Sally. "I've said so." See? \ / \\ // \\\ ///

System.out.println("\"Several Slashes are sometimes seen,\"\nsaid Sally. \"I've said so.\" See?"); System.out.println("\\ / \\\\ // \\\\\\ ///");

What is the output of the following loop? System.out.println("T-minus "); for (int i = 5; i >= 1; i--) { System.out.println(i + ", "); } System.out.println("Blastoff!");

T-minus 5, 4, 3, 2, 1, Blastoff!

What is the output produced from the following program? You may wish to draw a structure diagram first. public class Tricky { public static void main(String[] args) { message1(); message2(); System.out.println("Done with main."); } ​ public static void message1() { System.out.println("This is message1."); } ​ public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with message2."); } }

This is message1. This is message2. This is message1. Done with message2. Done with main.

What are the values of a, b, and c after the following code statements? (It may help you to write down their values after each line.) int a = 5; int b = 10; int c = b; a = a + 1; b = b - 1; c = c + a;

a = 6 b = 9 c = 16

What is the output produced from the following statements? (Treat tabs as aligning to every multiple of eight spaces.) System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the downward spiral");

a b c \\ ' """ C: in he downward spiral

Which of the following choices is the correct syntax for declaring a real number variable named 'grade' and initializing its value to 4.0? int grade : 4.0; grade = double 4.0; grade = 4; 4.0 = grade; double grade = 4.0;

double grade = 4.0;

What are the values of first and second at the end of the following code? int first = 8; int second = 19; first = first + second; second = first - second; first = first - second;

first = 19 second = 8

What are the values of i, j, and k after the following code statements? int i = 2; int j = 3; int k = 4; int x = i + j + k; i = x - i - j; j = x - j - k; k = x - i - k;

i = 4 j = 2 k = 1

Which of the following method headers uses the correct syntax? public static void example() { public static example void[] { public void static example() { public static example() { public void static example{} (

public static void example() {


Conjuntos de estudio relacionados

NU140- Chapter 38 Bowel Elimination

View Set

US - Hechos importantes de España

View Set

Ch. 24: The Fetal Head and Brain (Content)

View Set

FINA Chapter 8: Net Present Value & Other Investment Criteria

View Set