Lec5: Variable/Integer, Assignment Operators, Scanner Flashcards

1
Q

What will be displayed as a result of executing the following code?

int value1 = 9;
System.out.println(value1);
int value2 = 45;
System.out.println(value2);
System.out.println(value3);
value3 = 16;

A

Nothing, this is an error (did not declare the variable before printing it out).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the value of cube after the following code executes?

double cube, side;
side = 5.0;
cube = Math.pow(side, 3);

A

125.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which statement(s) is/are equivalent to the following?

A

number = number + 1;
number++;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What will be the values of x and y after running the following code?

int x = 25, y = 8;
x += y++;

A

x = 33, y = 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which statement(s) is/are equivalent to the following?

number = number * 2;

A

number *= 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is the value of the number after the following statements are executed?

int number = 10;
number += 5;
number -= 2;
number *= 3;

A

39

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following function(s) will return the value of x, rounded to the nearest whole number?

A

Math.round(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following represents the declaration and initialization of a variable named twoThirds, initialized to the floating-point constant two-thirds?

A

double twoThirds = 2/3.0;
double twoThirds = 2.0/3;
double twoThirds = (double)2/3;
double twoThirds = 2.0/3.0;
double twoThirds = 2/(double)3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If x and y are Java variables of type double, write the java code for the expression : x^2 + 2/3y -5

A

(Math.pow(x, 2) + 2) / (3 * y - 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which scanner class method would you use to read a string as input?

A

nextLine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the appropriate import statement to use when you plan on using the scanner in your program?

A

import java.util.*;
import java.util.Scanner;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give the data type of the result of the operation: Math.round(2, 6)

A

Long

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Consider the following variable:

int totalSeconds = 50000;

After you computed the number of whole hours this gives, which of the following statements would figure out the total number of leftover minutes, and then store it in the variable named minutes?

A

int minutes = totalSeconds % 3600 . 60;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Determine the output of the following code:

int bill = 11, emily = 3, omar;
omar = bill * –emily;
System.out.printf(“%3d%3d%3d\n”, bill, emily, omar);

A

11 2 22

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the value of the expression below?

Math.pow(Math.abs(3-6), Math.sqrt(4))

A

9

17
Q

Assume the variable declarations below for this problem:

int x = 3, y = 2, z = x + y;

Which of the following output statements correctly displays:

3 + 2 = 5

A

System.out.print(x + “+” + y “=” + z);

18
Q

What is the output from the following statements?

double x = 3.1415;
System.out.printf(“%8.3f\n”, x);

A

3.142

19
Q

Determine the output of the following code:

int kim = 6, kumail = 4, mick = 1;

mick += kim– + –kumail;
System.out.printf(“%3d%3d%3d\n”, kim, kumail, mick);

A

5 3 10

20
Q

Which of the following does not contain a syntax error?

A

num = a * (b + c);

21
Q

What will be the value of x after running the following code?

int x, y = 4, z = 6;
x = (y++) * (++z);

A

28

22
Q

Which of the following will NOT display the string “Welcome” followed by a newline character?

A

System.out.println(“%s\n”, “Welcome”);

System.out.printf(“%s”, “Welcome”);

System.out.printf(“s\n”, Welcome”);

System.out.print(“Welcome”);

23
Q

Which of the following statements will print out the character and NOT the integer for the following char declaration:

char fred = 102;

A

System.out.printf(%c\n”, fred);

24
Q

What will the following code print out?

int carlos = 81, maria = 20;

System.out.printf(“%d %d\n”, –carlos, maria++);

A

80 20

25
Q

What will be displayed after the following statements have been executed?

final double X = 61.1

X = 54.3;

System.out.println(“X = “ + X);

A

Nothing, this is an error. Permanently named constants should be declared and initialized in the same line.

26
Q

Determine the output of the following code:

int a = 9, b = 4, c = 23;
c %= b++;
a = c + b * 3 + a;
System.out.printf(“%3d%3d%3d\n”, a, b, c);

A

27 5 3