Printing Strings with the format Method in Java Flashcards
What is the purpose of the format
method in Java?
a) To define a new class
b) To print a string with formatting options
c) To declare variables
d) To store integer values
b) To print a string with formatting options
How are two strings combined in Java?
a) Using a minus symbol
b) By declaring them on the same line
c) By using the plus symbol (+)
d) By using the multiplication symbol (*)
c) By using the plus symbol (+)
What is a string literal in Java?
a) A variable that stores numbers
b) A sequence of characters defined in double quotes
c) A class method
d) A function that formats strings
b) A sequence of characters defined in double quotes
Can you concatenate a string and an integer in Java using the plus symbol?
a) No, only strings can be concatenated
b) Yes, but it requires casting the integer
c) Yes, Java automatically converts the integer
d) No, it results in a syntax error
c) Yes, Java automatically converts the integer
What is the purpose of the %d
placeholder in the format
method in Java?
a) To substitute a string value
b) To substitute a boolean value
c) To substitute an integer value
d) To substitute a float value
c) To substitute an integer value
In Java, how can you place a variable’s value in the middle of a string using format
?
a) Use the +
operator
b) Use %d
for integers or %s
for strings
c) It is not possible
d) Place the variable directly inside the string
b) Use %d
for integers or %s
for strings
What is the placeholder %s
used for in the format
method?
a) Substituting a float value
b) Substituting a string value
c) Substituting an integer value
d) Substituting a double value
b) Substituting a string value
How can you declare multiple integer variables on a single line in Java?
a) Separate variables with a semicolon
b) Separate variables with commas
c) Use multiple int
keywords
d) Declare one variable at a time
b) Separate variables with commas
Where are static variables typically defined in Java?
a) Inside methods
b) Inside constructors
c) Outside methods but inside a class
d) Inside loops
c) Outside methods but inside a class
What is a key characteristic of static variables in Java?
a) They are local to each method
b) They are shared by all instances of a class
c) They can only be assigned once
d) They are always private
b) They are shared by all instances of a class
In the following code, what is the value printed for the static variable?
public static int staticVar = 20; public static void main(String[] args) { int localVar = 10; System.out.println("Static variable value is: " + staticVar); }
a) 10
b) 0
c) 20
d) 30
c) 20
What is printed to the console when the following code is run?
~~~
public static String name = “Smith”;
public static void main(String[] args) {
int salary = 50000;
System.out.println(name + “ Salary is: “ + salary);
}
```
a) Smith Salary is: 50000
b) Smith Salary is:
c) Salary is: 50000
d) Smith 50000
a) Smith Salary is: 50000