Which of the following data types is the largest?
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
byte
t.gif
t.gif t.gif t.gif
short
t.gif
t.gif t.gif t.gif
int
t.gif
t.gif t.gif t.gif
long (*)
Which two are recommended practices for naming final variables?
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
Capitalize every letter () t.gif t.gif t.gif t.gif Separate words with an underscore ()
t.gif
t.gif t.gif t.gif
Capitalize first letter
t.gif
t.gif t.gif t.gif
Separate words with an space
What is the output?
public class Person {
public static void main(String args[]) {
int age = 20;
System.out.println(“Value of age: ” +age);
age = 5 + 3;
System.out.println(“Value of age: ” +age);
age = age + 1;
age++;
System.out.println(“Value of age: ” +age);
}
}
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
Value of age: 20
Value of age: 8
Value of age: 10 (*)
t.gif
t.gif t.gif t.gif
Value of age: 20
Value of age: 28
Value of age: 38
t.gif
t.gif t.gif t.gif
Value of age: 20
Value of age: 208
Value of age: 20810
t.gif
t.gif t.gif t.gif
Value of age: 20
Value of age: 8
Value of age: 9
Which two are valid assignments of a? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
int a; a = 10; () t.gif t.gif t.gif t.gif int a = 10; ()
t.gif
t.gif t.gif t.gif
int a = “10”;
t.gif
t.gif t.gif t.gif
int a = 10
Which two data types are appropriate for their variable? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
int averageDollarAmount = 19.95;
t.gif
t.gif t.gif t.gif
double checkingAmount = 1500; () t.gif t.gif t.gif t.gif String firstName = “Alex”; ()
t.gif
t.gif t.gif t.gif
boolean age = 20;
Which is valid syntax to declare and initialize a String variable? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
String x= “Java”; (*)
t.gif
t.gif t.gif t.gif
String “x” = Java;
t.gif
t.gif t.gif t.gif
String x = Java;
t.gif
t.gif t.gif t.gif
String “x” = “Java”;
Assuming x is an int, which of the following are ways to increment the value of x by 1? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
x = x +1;
t.gif
t.gif t.gif t.gif
x = +1;
t.gif
t.gif t.gif t.gif
x+;
t.gif
t.gif t.gif t.gif
x++;
t.gif
t.gif t.gif t.gif
x += 1;
How many bits are in a byte? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
2
t.gif
t.gif t.gif t.gif
4
t.gif
t.gif t.gif t.gif
6
t.gif
t.gif t.gif t.gif
7
t.gif
t.gif t.gif t.gif
8
Which keyword makes a variable’s value unchangeable? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
const
t.gif
t.gif t.gif t.gif
final
t.gif
t.gif t.gif t.gif
static
t.gif
t.gif t.gif t.gif
break
Identify the variable declared in the given code?
public class Welcome {
public static void main(String args[]) {
int a = 2;
System.out.println(“a is” + a);
}
} t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
int
t.gif
t.gif t.gif
t.gif
Welcome
t.gif
t.gif t.gif t.gif
2
t.gif
t.gif t.gif t.gif
a
Identify the names of two variables used in the given code.
public class Variables {
public static void main(String args[]) {
String strVal = “Hello”;
int intVal = 0;
System.out.println(“Integer: ” +intVal)
}
} t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
int
t.gif
t.gif t.gif t.gif
Hello
t.gif
t.gif t.gif t.gif
intVal
t.gif
t.gif t.gif t.gif
strVal
t.gif
t.gif t.gif t.gif
String
Which of the following two statements are true about variables? t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif (Choose all correct answers) t.gif
t.gif t.gif t.gif t.gif t.gif t.gif
t.gif t.gif t.gif
They make code becomes flexible.
t.gif
t.gif t.gif t.gif
Variables will be ignored by compiler.
t.gif
t.gif t.gif t.gif
The allow code to be edited more efficiently.
t.gif
t.gif t.gif t.gif
The value assigned to a variable may never change.
The correct answer and explanation is:
Answers and Explanations:
- Which of the following data types is the largest?
- long
longdata type is the largest among the listed types. It is 64 bits, whereasbyteis 8 bits,shortis 16 bits, andintis 32 bits. Therefore, thelongtype has the largest storage capacity. - Which two are recommended practices for naming final variables?
- Capitalize every letter
- Separate words with an underscore
- It’s a common convention in Java to use uppercase letters for final variables and constants (e.g.,
PIorMAX_SIZE). - Underscore separation is also recommended for readability in multi-word constants (e.g.,
MAX_SIZE,NUM_ITEMS). - Java doesn’t recommend capitalizing only the first letter or using spaces in variable names.
- What is the output?
public class Person { public static void main(String args[]) { int age = 20; System.out.println("Value of age: " + age); age = 5 + 3; System.out.println("Value of age: " + age); age = age + 1; age++; System.out.println("Value of age: " + age); } }- Value of age: 20
- Value of age: 8
- Value of age: 10
- Initially,
ageis set to 20. - The next line reassigns
ageto the result of5 + 3, which is 8. - The subsequent lines increment the value of
agefirst by 1 (age + 1), and then by 1 again (age++), so the final value ofageis 10.
- Which two are valid assignments of a?
- int a; a = 10;
- int a = 10;
- Both
int a; a = 10;andint a = 10;are valid. The first one declaresaand assigns a value later, while the second declares and assigns the value in one step. int a = “10”;is invalid because"10"is aString, not anint.
- Which two data types are appropriate for their variable?
- double checkingAmount = 1500;
- String firstName = “Alex”;
checkingAmountshould be adoublesince it represents a monetary amount and could have decimals.firstNameis correctly assigned aStringtype.int averageDollarAmount = 19.95;is invalid because19.95is a decimal and needs adouble.boolean age = 20;is incorrect becausebooleanvalues can only betrueorfalse.
- Which is valid syntax to declare and initialize a String variable?
- String x= “Java”;
- Assuming x is an int, which of the following are ways to increment the value of x by 1?
- x = x + 1;
- x++;
- x += 1;
xby 1 in Java.x = x + 1explicitly adds 1 tox,x++is a shorthand operator for incrementingxby 1, andx += 1is an abbreviated version ofx = x + 1. - How many bits are in a byte?
- 8
byteis defined as 8 bits in Java and many other programming languages. - Which keyword makes a variable’s value unchangeable?
- final
finalkeyword is used to declare constants, which means once a value is assigned to afinalvariable, it cannot be changed. - Identify the variable declared in the given code:
public class Welcome { public static void main(String args[]) { int a = 2; System.out.println("a is" + a); } }- a
a, and its value is set to 2. - Identify the names of two variables used in the given code:
public class Variables { public static void main(String args[]) { String strVal = "Hello"; int intVal = 0; System.out.println("Integer: " + intVal); } }- intVal
- strVal
strValandintVal. - Which of the following two statements are true about variables?
- They make code become flexible.
- They allow code to be edited more efficiently.
- Variables allow the code to be flexible because the values they hold can change throughout the program.
- They make code easier to maintain and update since you can modify the values of variables instead of hardcoding specific values multiple times.
Summary:
Variables and constants in Java are fundamental for creating dynamic and efficient code. Proper naming conventions and the understanding of data types, syntax, and operators help in making the code more readable, maintainable, and bug-free.