CMSC 131 Study Questions 2 Flashcards

1
Q

When your Java program is compiled, what type of file is created? (Hint: It is NOT machine language.)

A

bytecode

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

What does it mean for someone to say that a Java program is “portable”?

A

The same bytecode will run successfully on any platform (provided that the Java Virtual Machine (JVM) is available.)

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

TRUE/FALSE: Inserting unnecessary spaces and/or blank lines could cause your Java program to malfunction.

A

FALSE

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

What is the difference between “syntax errors” and “logical errors”?

A

Syntax errors are due to your failure to follow the rules of the language. Logical errors are where you have written a valid Java program, but it does not behave the way it should.

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

If your program compiles and runs, but behaves incorrectly, are you probably suffering from “syntax” or “logical” errors?

A

logical

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

If Eclipse flags your code with a red mark and won’t let you compile it, are you suffering from “syntax” or “logical” errors?

A

syntax

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

List the four Java primitive types that can be used to store integer values.

A

long, int, short, byte

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

How much memory is required to store each of the four types mentioned in the previous question?

A

long 8, int 4, short 2, byte 1

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

What advantage do you gain from using one of the types of integer types that requires MORE memory?

A

You can store a wider range of values (larger values).

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

List the two Java primitive types that can be used to store floating point values.

A

float and double

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

How much memory is required to store each of the two types mentioned in the previous question?

A

float 4, double 8

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

What advantage do you gain from using one of the floating point types that requires MORE memory?

A

more precision (it will retain more digits)

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

List the two Java types that are used to store values that are not numbers.

A

char and boolean

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

Write a statement that declares a variable named counter of type int, and stores the value 182 in the variable.

A

int conter = 182;

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

Write a statement that simultaneously declares three variables of type boolean , named x, y, and z.

A

boolean x, y, z;

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

What values can a boolean variable achieve?

A

true or false

17
Q

Write a java class called “Fred”. Put in a main method. Have the main method store your age in a variable named age. Then main should print out a line that has your name, followed by your age. (Use a “string literal” for your name, but use the variable to access your age.)

A
public class Fred {
      public static void main(String[] args) {
                  int age = 38;
                  System.out.println("Your name here, age is " + age);
      }
}
18
Q

Practice using BOTH styles for Java comments.

A

// comment using first style

/* comment using second style */

19
Q

Is the following statement valid: int x = 34.7;

A

NO – you can’t force Java to store a floating point value in an integer variable.

20
Q

Is the following statement valid: double y = 12;

A

YES

21
Q

Is the following statement valid: boolean q = 17 < 25;

A

YES

22
Q

Evaluate the following Java expression: 9 - 15 * 6 / 11 + 4

A

5

23
Q

Evaluate the following Java expression: 75 % 7

A

5

24
Q

Never forget that you should not compare two Strings with the == operator. Suppose you have two String variables, x and y. Give an expression that can be used to check whether or not the Strings x and y are identical.

A

x.equals(y)

25
Q

What is “concatenation”? What operator do you use to concatenate Strings?

A

Joining two Strings into one larger string. Use the + operator.

26
Q

What is the difference between System.out.print and System.out.println?

A

System.out.println moves the cursor down to the next line after it does it’s output.

27
Q

Suppose you have a String variable called s. What expression will return the number of characters in the String?

A

s.length()

28
Q

Give examples of “literals” of each of the following types: String, char, long, int, float, double, boolean.

A

String: “Hello”

char: ‘x’
long: 17L
int: 17
float: 3.14F
double: 3.14
boolean: true

29
Q

What “escape sequence” would you use in a String to indicate a “new line”?

A

\n