Unit 1 (Primitive Types) Flashcards
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)
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 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. 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.
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
D.
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.
Answer D
Correct. For the text that appears in parentheses to be interpreted as string literals, it must be enclosed in quotation marks.
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) **
**
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 **.
What is the * symbol?
Multiplication of doubles or ints
What is the ++ operator
adds 1 to variable
What is the – operator
subtracts 1 from a variable
What is the % operator
It is the modulo operator. It works as division but returns a remainder, not the quotient.
What is the / operator
The division operator. It returns a quotient
What is the result? Look carefully.
int x = 4;
int y = 3;
int z = y%x;
System.out.print(z);
3
What is the result? Look carefully.
int x = 4;
int y = 3;
int z = x%y;
System.out.print(z);
1
What are the Unary Operators?
Increment, Decrement, negation, affirmation
What is the priority tiering of the operators?
Parentheses
Unary operators
Multiplicative and fractional (*, /, %)
Additive and subtractive (+,-)
What is the sequence of operators?
left to right with the priority going first.