Chapter 1: Building blocks Flashcards

(41 cards)

1
Q

The java command launches the Java Virtual Machine (JVM) before running bytecode. If false, why?

A

True

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

What is type promotion in Java?

A

Type promotion automatically converts smaller or less precise types to larger or more precise types during expressions, so that operations can be performed safely without data loss.

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

The javac command converts .java source files into .class bytecode. If false, why?

A

True

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

The jar command is used to package multiple files together in Java. If false, why?

A

True

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

The javadoc command generates documentation for Java source code. If false, why?

A

True

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

A Java class can have both methods and fields. If false, why?

A

True

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

The void keyword in Java means a method does not return any value. If false, why?

A

True

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

Parameters in a method provide values from the calling method. If false, why?

A

True

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

The javac command is used to execute Java programs. If false, why?

A

False – The javac command compiles source code into bytecode, but does not execute it.

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

An object and a class in Java are the same thing. If false, why?

A

False – A class is a blueprint, while an object is an instance of that class.

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

A reference variable in Java holds the actual object. If false, why?

A

False – A reference variable holds the memory address of an object, not the object itself.

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

A Java class must always contain at least one method. If false, why?

A

False – A class can be empty, such as public class Animal {}.

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

The simplest Java class must include a main method. If false, why?

A

False – A Java class only needs to be defined; a main method is required only for execution.

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

The String class in Java is a primitive data type. If false, why?

A

False – String is a class, not a primitive type; it is an object in Java.

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

The public keyword restricts a method’s access to only within the same class. If false, why?

A

False – public allows the method to be accessed from other classes.

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

A single-line comment in Java starts with // and extends to the end of the line.

A

True

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

A multiline comment in Java starts with /** and ends with **/.

A

(Correct: It starts with /* and ends with */.)

18
Q

Javadoc comments can only be used inside methods.

A

(Correct: Javadoc comments can be used for classes, methods, and fields.)

19
Q

Java ignores all comments, including Javadoc comments, at runtime and compile time.

A

(Correct: Javadoc comments can be processed by the Javadoc tool.)

20
Q

A Java source file can contain multiple public classes.

A

(Correct: Only one top-level class can be public.)

21
Q

If a class is not declared public, it must be declared private.

A

(Correct: A top-level class can be package-private if no access modifier is specified.)

22
Q

A Java file with a public class does not have to match the filename.

A

(Correct: The filename must match the public class name exactly.)

23
Q

The Javadoc @author tag is required for all classes.

A

(Correct: It is optional and used for documentation purposes.)

24
Q

Comments can slow down Java code execution.

A

(Correct: Comments are ignored by the compiler and do not affect execution speed.

25
A Javadoc comment must include an @param tag.
(Correct: The @param tag is only needed if documenting method parameters.)
26
What happens if a line inside a text block ends with a backslash \?
The backslash suppresses the automatic line break and joins the next line into one continuous line.
27
Does the backslash \ at the end of a line in a text block insert a \n (newline) character?
No! It avoids inserting any \n. The next line is simply glued to the current one without any newlines.
28
In a Java text block, what is the effect of a backslash \ at the end of a line?
* No new line is inserted. * It creates one continuous line in the resulting String.
29
What is essential whitespace in a Java text block?
Essential whitespace is the part of the text block’s indentation or spaces that you intend to keep as part of the actual String content.
30
Does Java automatically remove essential whitespace inside a text block?
No! Essential whitespace is preserved exactly in the final String — it is important and intentional.
31
How does Java decide what whitespace is essential in a text block?
Java draws a vertical line based on the leftmost non-blank character; everything to the right of that line (including spaces) is essential and kept in the String.
32
What is incidental whitespace in a Java text block?
Incidental whitespace is extra indentation at the beginning of lines, added just to make the code easier to read, but not intended to be part of the actual String content.
33
What does Java do with incidental whitespace in a text block?
Java automatically removes incidental whitespace from each line when building the final String.
34
How does Java identify incidental whitespace inside a text block?
It looks for the smallest common indentation across all non-blank lines and removes that shared indentation.
35
Is variable interpolation (\\{} syntax) supported in Java 17 text blocks?
No. Java 17 text blocks do not support variable interpolation. You must manually concatenate values.
36
In Java 17, how do you include a variable inside a text block?
You must concatenate manually using +, like: ``` String name = "John"; String text = """ Hello """ + name + """ Welcome! """; ```
37
When did variable interpolation (\\{} syntax) officially become available in Java?
Variable interpolation was introduced as a preview feature in Java 21 with String Templates, not in Java 17.
38
In an operation between a long and a double, which type is promoted, and what is the result type?
The long is promoted to double, and the result is of type double.
39
List Java’s numeric promotion hierarchy (smallest to largest).
byte → short → int → long → float → double
40
What happens when you perform arithmetic (like +, -, *, /) between two char values in Java?
Both char values are promoted to int before the operation. The result of the operation is an int, not a char.
41
Why does Java promote char values to int during arithmetic operations?
Because char can only store one 16-bit character (0–65,535), but arithmetic operations might produce values larger than that. Promoting to int ensures no overflow or data loss.