Declaring Variables in Java Flashcards

1
Q

What does the keyword int specify when declaring a variable in Java?

a) The name of the variable
b) The value of the variable
c) The data type of the variable
d) The length of the variable

A

c) The data type of the variable

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

Why does the program throw an error if the variable number is declared but not initialized?

public class MyFirstProgram { 
   public static void main(String[] args) {
	    int number;
			System.out.println(number);
   }
}

a) Java doesn’t allow declaring variables without assigning them a value
b) The variable number is not being referenced
c) The variable number does not have a value to print
d) The semicolon is missing at the end of the line

A

c) The variable number does not have a value to print

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

What is the correct way to initialize a variable number with the value 20?

a) int number == 20;
b) int number = 20;
c) number = int 20;
d) number int = 20;

A

b) int number = 20;

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

What is the primary benefit of using variables in a program?

a) They prevent syntax errors
b) They allow you to store and reference values dynamically
c) They make the program easier to write
d) They automatically optimize the performance

A

b) They allow you to store and reference values dynamically

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

What is the proper naming convention for variables in Java?

a) PascalCase
b) camelCase
c) Uppercase letters only
d) Lowercase letters only

A

b) camelCase

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

Why did IntelliJ mark the variable varint with a green underline?

a) It was a syntax error
b) The variable varint had not been initialized
c) It suggested a change to match camelCase convention
d) It detected a naming conflict with Java keywords

A

c) It suggested a change to match camelCase convention

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

What is the difference between varint and varInt in Java?

a) There is no difference; variable names are not case-sensitive in Java
b) varint and varInt refer to different variables since Java is case-sensitive
c) varInt is a reserved keyword in Java
d) Both are incorrect variable names

A

b) varint and varInt refer to different variables since Java is case-sensitive

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

What happens if you declare a variable but do not initialize it in Java?

a) The program will throw an error at runtime
b) The variable is automatically assigned a default value
c) The program will run but may produce unpredictable results
d) The variable cannot be used until it is initialized

A

d) The variable cannot be used until it is initialized

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

What is a key characteristic of Java that makes it different from languages like Python regarding variable declarations?

a) Variables can be used without being declared
b) Java is a loosely-typed language
c) Variables must be declared with a specific type before assignment
d) Java allows for dynamic typing

A

c) Variables must be declared with a specific type before assignment

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

What happens if you try to assign a new value to a variable declared as final?

a) The program will allow the change
b) The compiler will throw an error
c) The old value will be replaced
d) The program will run but issue a warning

A

b) The compiler will throw an error

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

In the example with varInt, why is only the value 50 printed to the console?

public class MyFirstProgram {
   public static void main(String[] args) {
        int varInt = 100;
        varInt = 50;
        System.out.println(varInt);
    }
}

a) The value of 50 was assigned last, and a variable can hold only one value at a time
b) The program encountered an error and skipped the earlier value
c) The variable varInt cannot hold values larger than 50
d) Java does not allow variables to be reassigned

A

a) The value of 50 was assigned last

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

What is the purpose of using the final keyword when declaring a variable in Java?

a) To make the variable accessible only in the current class
b) To prevent the variable from being reassigned
c) To make the variable private
d) To indicate that the variable is a global constant

A

b) To prevent the variable from being reassigned

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