Unit 1 (Primitive Types) Flashcards

1
Q

How do you print a variable? (x is predefined)

a) System.print(x);
b) System.out.print(x);
c) System.out(x);
d) System.out.print(x)

A

B. This is because in Java, you must include the calling of the System, the direction of input or output, and the print statement. D does fit the criteria but does not include a semicolon, a necessary form of syntax to define the end of a line or function.

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

How do you print a string?
A) System.out.print(“This is a statement”);
B) System.out.print(This is a statement);
C) System.print(This is a statement);
D) system.out(“This is a statement”);
E) system.out.print(“This is a statement”);

A

A. This is because in Java, you must include the calling of the System, the direction of input or output, and the print statement. Quotations must also be included as they define the string.

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

Consider the following code segment.

System.out.print(“AP”);
System.out.println();
System.out.println(“CS”);
System.out.print(“A”);

What is printed as a result of executing the code segment?

A) APCSA

B) APCS
A

C) AP
CSA

D)AP
CS
A

E) AP
CS
A

A

D.

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

Consider the following code segment.

System.out.print(I do not fear computers. );   // Line 1
System.out.println(I fear the lack of them.);  // Line 2
System.out.println(--Isaac Asimov);            // Line 3

The code segment is intended to produce the following output but may not work as intended.

I do not fear computers. I fear the lack of them.
--Isaac Asimov

Which change, if any, can be made so that the code segment produces the intended output?

A) In line 1, print should be changed to println.

B) In lines 2 and 3, println should be changed to print.

C) The statement System.out.println() should be inserted between lines 2 and 3.

D) In lines 1, 2, and 3, the text that appears in parentheses should be enclosed in quotation marks.

E) No change is needed; the code segment works correctly as is.

A

Answer D
Correct. For the text that appears in parentheses to be interpreted as string literals, it must be enclosed in quotation marks.

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

Consider the following code segment.

System.out.print("*");
System.out.println("**");
System.out.println("***");
System.out.print("****");

What is printed as a result of executing the code segment?

A) *
**
***
**

B) *
**
***

C) *
*****
**

D) **
**

**

E) **
**

A

Answer D.
Correct. The arguments of the first and second statements appear on the first line of output, since the print statement in line 1 of the code segment does not move the cursor to the next line before the println statement in line 2 of the code segment is executed. This produces a line of output containing ** and then moves the cursor to the next line. The println statement in line 3 of the code segment produces another line of output containing ** and then moves the cursor to the next line. The final print statement produces a third line of output that contains **.

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

What is the * symbol?

A

Multiplication of doubles or ints

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

What is the ++ operator

A

adds 1 to variable

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

What is the – operator

A

subtracts 1 from a variable

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

What is the % operator

A

It is the modulo operator. It works as division but returns a remainder, not the quotient.

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

What is the / operator

A

The division operator. It returns a quotient

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

What is the result? Look carefully.

int x = 4;
int y = 3;
int z = y%x;
System.out.print(z);

A

3

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

What is the result? Look carefully.

int x = 4;
int y = 3;
int z = x%y;
System.out.print(z);

A

1

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

What are the Unary Operators?

A

Increment, Decrement, negation, affirmation

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

What is the priority tiering of the operators?

A

Parentheses
Unary operators
Multiplicative and fractional (*, /, %)
Additive and subtractive (+,-)

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

What is the sequence of operators?

A

left to right with the priority going first.

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

What is the result and explain?

int x = 3;
double y = 5;

double z = y/x;
double w = (int) y/x;
double v = (int)(y/x);
int m = y/x;
System.out.print(z+”, “+ w+”, “+v+”, “+m);

A

Output:

1.66666, 1.0, 1.0, 1

Explanation:

This is because when dividing double by int or vice versa, you will always get a double as an answer as the output is promoted to the class of double. Then, when cast as type int, it will truncate the result and make it 1. If it is then further defined as a double, it will become 1.0.

17
Q

how can you assign multiple variables in one line?

A

int a = 1, b = 2, c = 3;
OR
double a = 1, b = 2, c = 3;

18
Q

what are the restrictions on naming variables?

A

Cannot use Java Keywords
No spaces
Camel Case (ex. firstNameNum)

19
Q

Name all the java keywords

A

abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, double, do, else, enum, extends, false, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, true, try, void, volatile, while

20
Q

What is wrong with this code segment?

double x = 3;
int y = 5;

int w = y/x;
System.out.print(w);

A

The operation of y/x must be cast to int because the value is assigned to double and must be cast to int. So, the operation should look like this:

int w = (int)(y/x)

This is because y is already an int and the whole operation must be enclosed in parentheses to make the code work. Or, you can also use this snippet as x is a double so it will make int/int division.

int w = y/(int)x

21
Q

say the main method.

A

public static void main(String[] args)
{

}

22
Q

Say the class method.

A

public class Main{
public static void main(String[] args)
{

} }
23
Q

What is an operand?

A

The things being operated on. Ex.
int x = 1, y = 2;
int w = x/y;

Here, x and y are the operands. The operator is the /

24
Q
A