Font size
WorksheetsJava Variables and Naming Conventions Quiz
Total questions: 128
Worksheet time: 1hrs 4mins
Which of the following is NOT a type of variable mentioned in the content?
Primitive variable
Reference variable
Instance variable
Global variable
According to the content, which statement is true about Java Naming Convention?
It is a guideline for naming identifiers in Java.
It is a rule for assigning values to variables.
It is a method for declaring constants.
It is a way to create assignment statements.
Which of the following is an example of a final variable in Java?
A variable whose value cannot be changed after initialization.
A variable that stores reference to an object.
A variable declared inside a method.
A variable that holds primitive data types.
If you want to store a value that should not change throughout the execution of a Java program, which concept from the content would you use?
Constant
Instance variable
Assignment statement
Reference variable
Which type of variable is typically declared inside a method and only accessible within that method?
Local variable
Instance variable
Reference variable
Final variable
How would you apply the concept of assignment statement in Java to initialize a variable?
By using the equals sign (=) to assign a value to a variable.
By declaring a variable as final.
By following Java naming conventions.
By creating a constant.
Which of the following is a requirement when writing identifiers in Java?
Follow Java naming conventions.
Use only uppercase letters.
Start with a number.
Include special characters.
What is the correct procedure to declare a variable in Java?
Specify the data type followed by the variable name.
Write the variable name followed by the data type.
Use only the variable name.
Declare variables without specifying a type.
After declaring a variable in Java, what is the next step to use it in a program?
Assign a value to the variable.
Delete the variable.
Rename the variable.
Ignore the variable.
Suppose you are writing a Java program and need to store a user's age. Which of the following steps should you follow to ensure proper variable usage? (Select the best sequence)
Declare an appropriate variable, then assign a value to it.
Assign a value before declaring the variable.
Use the variable without declaring or assigning a value.
Declare the variable and never assign a value.
What are the names given to classes, variables, and methods in Java called?
Identifiers
Functions
Keywords
Operators
Which of the following best describes a Java identifier?
A name that identifies elements such as classes, methods, and variables in a program
A reserved word used by the Java compiler
A type of data structure in Java
A special symbol used in Java syntax
Why are there rules for what constitutes a legal Java identifier?
To ensure that names for classes, methods, and variables are valid and recognized by the Java compiler
To make Java code run faster
To allow the use of any character in names
To prevent the use of numbers in variable names
Why might Java programmers follow naming conventions beyond what is legally required by the language?
To improve code readability and maintainability
To make the code run faster
To avoid using any reserved words
To reduce the size of the compiled program
Suppose you are designing a Java program with several classes and methods. How would following naming conventions for identifiers benefit your team during collaborative development?
It would make the code easier to understand and maintain for all team members.
It would automatically optimize the program’s performance.
It would prevent all syntax errors in the code.
It would allow the use of any language for naming.
Which of the following is a valid starting character for a Java identifier?
Number
Dollar sign ($)
Space
Hyphen (-)
After the first character, which of the following can Java identifiers contain?
Only letters
Only numbers
Letters, currency characters, connecting characters, or numbers
Only currency characters
What is the limit to the number of characters an identifier can contain in Java?
10 characters
255 characters
No limit
1000 characters
Which of the following can NOT be used as a Java identifier?
myVariable
true
_value
$amount
If you declare two identifiers in Java, "uthm" and "Uthm", what is true about them?
They refer to the same variable
They are different identifiers
They are both invalid
They are both reserved keywords
Why can't you use a Java keyword as an identifier?
Because keywords are reserved for specific language functions
Because keywords are too long
Because keywords are case-insensitive
Because keywords must start with a number
Suppose you want to create a Java identifier for a variable that stores a user's age. Which of the following would be a valid identifier?
1age
age_user
null
user-age
Which of the following is a valid identifier in most programming languages like C or Java?
int _a;
int .f;
int 2_w;
int -d;
Which identifier is NOT valid according to typical programming language rules?
int $c;
int 2_w;
int _a;
int _$;
Why is "int 2_w;" not a valid identifier in most programming languages?
Identifiers cannot start with a digit.
Identifiers cannot contain underscores.
Identifiers must end with a semicolon.
Identifiers cannot contain letters.
Which of the following identifiers is valid and follows the rules for naming variables?
int this_is_a_detailed_name_for_identifier;
int .f;
int -d;
int 7g;
Select the identifier that is invalid because it contains a special character not allowed in most programming languages.
int e#;
int _$;
int _a;
int this_is_a_detailed_name_for_identifier;
Given the identifier "int _$;", which of the following best explains why it is valid?
Identifiers can start with an underscore or a dollar sign.
Identifiers must start with a digit.
Identifiers cannot contain underscores.
Identifiers must contain at least one number.
If you were to create a new variable name, which of the following would be the best practice to ensure it is valid?
Start with a letter or underscore, and use only letters, digits, or underscores.
Start with a digit and use any special character.
Use spaces to separate words in the identifier.
Use a period (.) to start the identifier.
Which of the following is a valid identifier in most programming languages?
2_w
.f
__a
-d
Why is the identifier "2_w" considered invalid?
It contains a hyphen
It starts with a digit
It contains a dollar sign
It contains an underscore
Which character is NOT allowed in identifiers according to the rules provided?
Underscore (_)
Dollar sign ($)
Dot (.)
Letter (a-z)
Given the identifier "int this_is_a_detailed_name_f_or_identifier;", why is it considered valid?
It contains a hash sign (#)
It starts with a digit
It uses only allowed characters (letters and underscores)
It contains a hyphen (-)
If you want to create a valid identifier, which of the following rules must you follow?
It can start with any character, including digits
It can contain illegal characters like dot (.) and hyphen (-)
It must start with a letter, underscore, or dollar sign
It must contain at least one digit
Strategic Thinking: You are reviewing code and see the identifier "int e#;". Based on the rules, what is the best explanation for why this identifier is invalid?
It starts with a digit
It contains a hash sign (#), which is not allowed
It contains a dollar sign ($), which is not allowed
It is too long
Skill/Concept: If you are given the identifier "int $c;", how would you determine if it is valid or not?
Check if it starts with a digit
Check if it contains only allowed characters (letters, digits, underscores, dollar signs)
Check if it contains a dot (.)
Check if it contains a colon (:)
Strategic Thinking: You need to create an identifier for a variable that stores a user's age. Which of the following would be the best choice?
7g
user-age
user_age
user.age
According to Oracle's Java naming conventions, what should be true about the first letter of a class or interface name?
It should be lowercase.
It should be capitalized.
It should be a number.
It should be an underscore.
What is the recommended naming convention for multi-word class or interface names in Java?
All words should be lowercase.
Only the first word should be capitalized.
The first letter of each inner word should be uppercase (camelCase).
Use underscores between words.
Which of the following is an example of a properly named Java class according to Oracle's recommendations?
animal
accountNumber
PrintWriter
serializable
Why are class names in Java typically nouns, while interface names are typically adjectives?
Because classes represent objects and interfaces represent behaviors.
Because classes are always abstract and interfaces are always concrete.
Because classes are used for variables and interfaces for methods.
Because classes are written in uppercase and interfaces in lowercase.
Given the following names, which one best follows the Java interface naming convention?
Animal
Account
Runnable
PrintWriter
If you are creating a Java class to represent a bank account, which of the following names best follows the naming conventions described?
bankaccount
BankAccount
bankAccount
BANKACCOUNT
According to Java naming conventions for methods, how should the first letter of a method name be written?
Uppercase
Lowercase
In bold
In italics
Which of the following is the correct naming convention for Java methods?
getbalance
GetBalance
getBalance
get_balance
Given the following method names, which one does NOT follow the Java naming convention described?
doCalculation
setCustomerName
CalcArea
getBalance
Which naming convention is recommended for Java variables?
camelCase
PascalCase
snake_case
UPPERCASE
According to Oracle, what type of names should be used for Java variables?
Short and meaningful names
Long and complex names
Randomly generated names
Names with special characters
Which of the following is a correct example of a Java variable name following the recommended convention?
buttonWidth
Buttonwidth
button_width
BUTTONWIDTH
Why is it important to start Java variable names with a lowercase letter when using camelCase?
It distinguishes variables from class names and follows Java conventions.
It makes the code run faster.
It allows variables to be used globally.
It is required for all programming languages.
Which of the following is the correct way to create a constant in Java?
By marking a variable as static and final
By marking a variable as public and private
By marking a variable as abstract and volatile
By marking a variable as synchronized and transient
According to Java naming conventions, how should constants be named?
Using uppercase letters with underscore characters as separators
Using lowercase letters with hyphens as separators
Using camelCase notation
Using PascalCase notation
Given the following variable names, which one follows the Java naming convention for constants?
minHeight
Min_Height
MIN_HEIGHT
min_height
Why is it important to use uppercase letters and underscores when naming constants in Java?
It distinguishes constants from variables and improves code readability
It makes the code run faster
It is required for all variable names in Java
It allows the constant to be changed at runtime
Which of the following identifiers follows the Java naming convention for class names?
MyCar
run()
address
YEAR
According to Java naming conventions, which identifier is most appropriate for a method name?
Submit()
LECTURER
ObjectOrientedProgramming
YEAR
Which of the following variable names best follows the Java naming convention for variables?
address
StopTheCar()
LECTURER
ObjectOrientedProgramming
Which of the following identifiers does NOT follow the Java naming convention for class names?
LECTURER
ObjectOrientedProgramming
MyCar
StopTheCar()
Evaluate the following identifier: "StopTheCar()". Is it following the Java naming convention for method names? Justify your answer.
Yes, because method names should use camelCase and start with a lowercase letter.
No, because method names should be in all uppercase.
Yes, because method names should start with an uppercase letter.
No, because method names should not use parentheses.
Given the variable names "address", "Name", and "YEAR", which one(s) do NOT follow the Java naming convention for variables?
Name and YEAR
address and Name
YEAR only
address only
Which of the following identifiers follows the PascalCase naming convention?
MyCar
mycar
MYCAR
myCar
Why does the identifier "LECTURER" not follow the class naming convention?
Because it uses all uppercase, which is convention for constants, not classes.
Because it uses PascalCase.
Because it starts with a lowercase letter.
Because it contains numbers.
What is the main characteristic of PascalCase as used in class naming conventions?
Each word starts with a capital letter.
All letters are uppercase.
All letters are lowercase.
Words are separated by underscores.
Given the identifier "ObjectOrientedProgramming", does it follow the class naming convention? Explain your reasoning.
Yes, because it uses PascalCase.
No, because it uses all uppercase.
No, because it uses underscores.
Yes, because it uses all lowercase.
Which of the following identifiers follows the camelCase naming convention?
Submit()
run()
StopTheCar()
RunTheCar()
Why does the identifier "Submit()" not follow the camelCase convention?
It contains numbers
It starts with an uppercase letter
It is too short
It uses underscores
If you want to correct the identifier "StopTheCar()" to follow camelCase convention, which of the following should you use?
stopthecar()
Stopthecar()
stopTheCar()
StopTheCar()
Suppose you are reviewing code and see the identifier "CalculateSum()". Based on the conventions discussed, what change would you recommend?
Change it to calculateSum()
Change it to Calculate_Sum()
Change it to calculatesum()
Leave it as is
Which identifier follows the camelCase naming convention for a single word?
address
Name
YEAR
MAX_YEAR
Why does the identifier "Name" not follow the naming convention?
It uses all lowercase letters.
It starts with an uppercase letter.
It uses all uppercase letters.
It contains special characters.
According to naming conventions, when should all uppercase letters be used for an identifier?
For regular variables
For constants
For function names
For class names
Given the identifier "YEAR", how should it be written to follow the convention for a regular variable?
YEAR
Year
year
maxYear
If you are naming a variable to store a client's name, which of the following is the best choice according to camelCase convention?
ClientName
clientName
CLIENTNAME
clientname
Which of the following best describes the purpose of variables in a program?
To store data in a program.
To perform calculations automatically.
To display output to the user.
To connect to the internet.
How many types of variables are there in Java?
Two
Three
Four
One
Which of the following is NOT a type of variable in Java?
Constant variable
Primitive variable
Reference variable
Suppose you are designing a Java program that needs to store both simple data values and objects. Which types of variables would you use, and why?
Primitive variables for simple data and reference variables for objects, because primitive variables store basic data types and reference variables store addresses to objects.
Reference variables for simple data and primitive variables for objects, because reference variables are faster.
Only primitive variables, because Java does not support reference variables.
Only reference variables, because primitive variables are obsolete.
Which of the following is NOT a primitive data type in Java?
char
boolean
String
int
Once a primitive variable has been declared in Java, what can never change?
Its value
Its primitive type
Its memory address
Its scope
Primitive variables in Java can be declared as which of the following? (Select the most complete answer.)
Only as local variables
Only as class variables
As class variables, instance variables, method parameters, or local variables
Only as method parameters
Which of the following is a correct example of declaring multiple primitive variables of the same type in a single line?
int x, y, z;
int x = y = z;
int x; y; z;
int(x, y, z);
Suppose you want to declare a variable to store the area of a circle using a primitive type. Which declaration is most appropriate?
double area;
boolean area;
char area;
String area;
Explain why the primitive type of a variable cannot be changed after declaration, but its value can usually be changed. Provide an example to support your explanation.
The primitive type is fixed at compile time, but the value can be reassigned during program execution. For example, 'int x = 5; x = 10;' is valid, but 'int x = 5; x = 10.5;' is not.
The primitive type can be changed at runtime, but the value cannot be changed.
Both the primitive type and value can be changed at any time.
Neither the primitive type nor the value can be changed after declaration.
What is the primary purpose of a reference variable in programming?
To refer to (or access) an object.
To store primitive data types.
To perform arithmetic operations.
To define a function.
Once a reference variable is declared to be of a specific type, what can be said about its type?
Its type can never be changed.
Its type can be changed at runtime.
Its type is determined by the value assigned.
Its type is always integer.
Which of the following is NOT a valid way to declare reference variables?
int x, y;
Object o;
String s1, s2, s3;
Car proton, audi;
Reference variables can be declared as which of the following? (Select the most complete answer.)
Static variables, instance variables, method parameters, or local variables.
Only static variables.
Only local variables.
Only method parameters.
A reference variable declared as "Animal myNewPetReferenceVariable;" can refer to which of the following?
Any object of type Animal or its subtype.
Only objects of type String.
Only primitive data types.
Any object, regardless of type.
Suppose you want to declare three reference variables of type String in a single line. Which of the following is the correct way?
String s1, s2, s3;
String; s1 s2 s3
String: s1 s2 s3;
String s1 s2 s3;
Which of the following best describes an instance variable in a class?
A variable defined inside the class but outside any method, and initialized when the class is instantiated.
A variable defined inside a method and shared by all objects.
A variable defined outside the class and accessible globally.
A variable that cannot be accessed by any object.
In the code example provided, what is the instance variable for the Circle class?
radius
Circle
Object
double
What is the main purpose of an instance variable in an object-oriented class?
To store unique values for each object instance.
To store values shared by all instances of the class.
To define the methods of the class.
To initialize the class header.
Which terms are considered virtually synonymous in the context of object-oriented programming?
Field, instance variable, property, attribute
Method, function, procedure, loop
Class, object, interface, package
Header, footer, body, tail
Suppose you have a class with an instance variable called "age." If you create two objects from this class and set their "age" values to 20 and 30 respectively, what will be the value of "age" for each object?
Each object will have its own unique "age" value (20 and 30).
Both objects will share the same "age" value.
The "age" value will be set to 0 for both objects.
The "age" value will be undefined for both objects.
Which of the following is a common practice when naming a parameter in a method that assigns its value to an instance variable?
Naming the parameter differently from the instance variable
Naming the parameter with the same name as the instance variable
Using only uppercase letters for parameter names
Avoiding parameters altogether
What is the main issue with the following code implementation? class Try { int size; public void setSize(int size) { size = size; // but which size equals which size? } }
The method does not return a value
The assignment is ambiguous and does not set the instance variable
The code uses an undefined variable
The method name is incorrect
Why does the code 'size = size;' inside the setSize method produce an error or unexpected behavior?
It tries to assign a value to a constant
It causes a syntax error due to missing semicolon
It assigns the parameter to itself, not the instance variable
It uses an undeclared variable
(DoK Level 1) What keyword is used in Java to resolve naming collisions between instance variables and parameters?
this
super
static
final
Which of the following is not a valid Java identifier?
_myVar
2ndValue
value_2
$amount
What is the main reason why the identifier int class; is invalid in Java?
It starts with a lowercase letter
It is too short
It uses a reserved keyword
It contains a special character
Which of the following statements about Java identifiers is true?
Identifiers must always start with a digit
Identifiers are case-sensitive
Identifiers cannot contain underscores
Identifiers can contain spaces
Choose the valid identifier from the following options:
int @value;
int 1stValue;
int value$1;
int #number;
Which of the following is the correct way to declare a constant in Java?
final int MAX_SIZE = 100;
constant int MAXSIZE = 100;
const int maxSize = 100;
static int MAX_SIZE = 100;
What will happen if you try to use a Java keyword as an identifier?
The identifier will be ignored
The program will compile successfully
The program will throw a runtime exception
The compiler will generate a syntax error
Which of the following is the recommended naming convention for Java constants?
MAX_VALUE
Max_Value
maxValue
MAXVALUE
What is the correct way to name a package in Java?
Com.Example.Project
com.example.project
com_example_project
com.Example.Project
Which of the following variable names follows Java's naming conventions?
account_balance
AccountBalance
ACCOUNTBALANCE
accountBalance
Which of the following is NOT a recommended practice for naming Java classes?
Avoid using underscores
Capitalize the first letter of each word
Start class names with a lowercase letter
Use nouns for class names
What is the main reason for using camelCase in Java variable and method names?
To avoid compiler errors
To make code look professional
To follow C++ conventions
To improve readability
Which of the following interface names best follows Java naming conventions?
serializable
serializableInterface
ISerializable
Serializable
Which of the following is a valid Java identifier?
2ndValue
value#2
_secondValue
class
What is the correct way to declare a variable named 'totalAmount' in Java?
int totalAmount;
int total-amount;
int 1totalAmount;
int total amount;
Which of the following statements about Java identifiers is TRUE?
Identifiers are case-sensitive.
Identifiers can contain spaces.
Identifiers can use any special character.
Identifiers can start with a digit.
Choose the invalid identifier from the following options.
_myVar
myVar$
my Var
myVar2
Which of the following is NOT allowed in a Java identifier?
Dollar sign ($)
Space
Underscore (_)
Alphabets
What will happen if you use 'public' as an identifier in Java?
The program will run but give a warning.
'public' will be treated as a variable name.
An error will occur because 'public' is a reserved keyword.
The code will compile successfully.
Which of the following is a valid Java identifier?
my-variable
_myVariable
class
2ndValue
What is the recommended naming convention for Java constants?
UPPER_CASE_WITH_UNDERSCORES
PascalCase
lowercase
camelCase
Which of the following statements about Java class names is correct?
Class names can contain spaces.
Class names should start with a lowercase letter.
Class names should start with an uppercase letter.
Class names must be written in all uppercase letters.
Which option best describes the purpose of using camelCase in Java variable names?
To make variable names shorter
To allow spaces in variable names
To ensure variables are always uppercase
To make variable names easier to read and understand
Which of the following is NOT allowed in a Java identifier?
Space ( )
Digits (0-9)
Dollar sign ($)
Underscore (_)
What is the correct way to declare a final variable in Java?
int final MAX_VALUE = 100;
final int MAX_VALUE = 100;
int MAX_VALUE final = 100;
MAX_VALUE final int = 100;
Which of the following is NOT allowed in a Java variable name?
Numbers (after the first character)
Space ( )
Dollar sign ($)
Underscore (_)
What is the recommended naming convention for Java constants?
lowercasewithunderscores
UPPERCASE_WITH_UNDERSCORES
PascalCase
camelCase
Which of the following is a valid Java identifier?
class
my-variable
2ndVariable
_myVariable
What is the purpose of using descriptive names for variables in Java?
To avoid using comments in code.
To make code easier to understand and maintain.
To increase the execution speed of the program.
To reduce the size of the compiled file.
Which of the following statements about Java identifiers is true?
They can contain spaces.
They can start with a digit.
They are case-sensitive.
They must be written in uppercase.
What is the result of the following Java statement?int totalAmount = 100;
An error occurs because the variable name is invalid.
A constant named totalAmount is created.
A variable named totalAmount is declared and assigned the value 100.
The value 100 is printed to the console.
