lec 2 Flashcards
(32 cards)
How do you print a variable in Java?
System.out.println(variableName);
How do you concatenate a variable with text in System.out.println()?
Use +, e.g., System.out.println(“Age: “ + age);
What happens when + is used between two numbers?
It performs addition.
What happens when + is used between a string and a number?
It concatenates them into a string.
How do you declare a constant variable?
Use the final keyword, e.g., final double PI = 3.14159;
What happens if you try to reassign a value to a final variable?
It causes a compilation error.
What are Java identifiers?
Names used for variables, methods, classes, etc.
What are the rules for naming identifiers?
They must start with a letter, _, or $, and cannot be a reserved word
How should Java constants be named?
Using uppercase letters with underscores, e.g., MAX_VALUE.
What must every Java program be contained within?
A class.
What is the naming rule for Java files?
The Java file name must match the class name and end with “.java”.
What is the purpose of System.out.println();?
It prints a line of text to the screen.
What must every Java statement end with?
A semicolon (;).
How does Java treat case sensitivity?
Java is case-sensitive; MyClass and myclass are different.
What is the purpose of the main() method in Java?
It serves as the entry point for execution.
What is the syntax for the main() method?
public static void main(String[] args)
What are the two types of comments in Java?
Single-line (//) and multi-line (/* … */).
How do you write a single-line comment?
Use // before the comment.
How do you write a multi-line comment?
Use /* … */ to enclose multiple lines of comments.
What are variables in Java?
Containers for storing data values.
Name the five main variable types in Java.
String, int, float, char, and boolean.
What data type would you use to store a whole number?
int.
What data type would you use to store a decimal number?
float or double.
What data type would you use to store a single character?
char.