Printing Strings with the format Method in Java Flashcards

1
Q

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

A

b) To print a string with formatting options

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

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 (*)

A

c) By using the plus symbol (+)

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

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

A

b) A sequence of characters defined in double quotes

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

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

A

c) Yes, Java automatically converts the integer

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

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

A

c) To substitute an integer value

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

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

A

b) Use %d for integers or %s for strings

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

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

A

b) Substituting a string value

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

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

A

b) Separate variables with commas

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

Where are static variables typically defined in Java?

a) Inside methods
b) Inside constructors
c) Outside methods but inside a class
d) Inside loops

A

c) Outside methods but inside a class

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

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

A

b) They are shared by all instances of a class

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

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

A

c) 20

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

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

a) Smith Salary is: 50000

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