Sample questions
Question 1Medium
Which one of the following will declare an array and initialize it with five numbers?
Array a = new Array(5);
int [] a = {23,22,21,20,19};
int a [] = new int[5];
int [5] array;
Answer: B.
Explanation
B is the legal way to declare and initialize an array with five elements.
A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object. C is wrong because it shows a legal array declaration, but with no initialization.
D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.
Step-by-step solution
- Option A creates an object of a class named Array, not an array.
- Option C declares an array of five slots but gives no values.
- Option D is not valid, because a size is not allowed in a declaration.
- int [] a = {23,22,21,20,19} declares and fills five numbers, which is option B.
Why the other options are wrong
- A. New Array(5) creates a class object, not an array of numbers.
- C. It creates five default zeros, not the five numbers asked for.
- D. A size is not allowed in the declaration, so it will not compile.
Tip: To declare and fill an array at once, write type name[] = {values}; never give a size in the declaration.
Question 2Medium
Which one is a valid declaration of a boolean?
boolean b1 = 0;boolean b2 = 'false';boolean b3 = false;boolean b4 = Boolean.false();
Answer: C. boolean b3 = false;
Explanation
A boolean can only be assigned the literal true or false.
Step-by-step solution
- Java boolean variables take only the literal true or false.
- Boolean b3 = false; uses the keyword false, which is valid.
- 0 is an int, and Java does not convert it to boolean.
- The valid declaration is boolean b3 = false;
Why the other options are wrong
- A. Java does not convert 0 to boolean; use the keyword false.
- B. Single quotes make a char literal; 'false' is not the keyword.
- D. Boolean has no false() method; false is a keyword literal.
Tip: In Java, boolean values are only true and false, with no quotes.
Question 3Medium
Which is a valid keyword in java?
- interface
- string
- Float
- unsigned
Answer: A. interface
Explanation
interface is a valid keyword.
B is wrong because although "String" is a class type in Java, "string" is not a keyword.
C is wrong because "Float" is a class type. The keyword for the Java primitive is float.
D is wrong because "unsigned" is a keyword in C/C++ but not in Java.a
Step-by-step solution
- interface is a reserved keyword in Java.
- String is not a keyword; String is a class name, and Java is case sensitive.
- Float is a class name; the primitive keyword is float in lowercase.
- unsigned is a keyword in C and C++, not in Java.
- So option A, interface, is the correct answer.
Why the other options are wrong
- B. String is not a reserved word; String is a class and Java is case sensitive.
- C. Float is a wrapper class; the keyword is the lowercase float.
- D. Unsigned is not a Java keyword.
Tip: Java keywords are all lowercase, such as int, float, class and interface.
Question 4Medium
Which of these values can a boolean variable contain?
- True & False
- 0 & 1
- Any integer value.
- Both a & b
Answer: A. True & False
Explanation
Boolean variable can contain only one of two possible values, true and false.
Step-by-step solution
- Boolean variables represent logical states with only two values: True and False.
- In many programming languages, these are also represented as 0 and 1 respectively.
- However, the actual allowed values for a boolean variable are strictly True and False.
- Option A lists True & False which matches the correct values for a boolean variable.
- Final answer: True & False.
Why the other options are wrong
- B. Java boolean variables cannot hold 0 or 1; those are int values, not boolean values.
- C. A boolean holds only true or false, not any integer value.
- D. Both is wrong because 0 and 1 are not valid boolean values in Java.
Tip: Remember: Boolean is binary but uses keywords, not digits.
Question 5Medium
What is the output of this program?
class conversion
{
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
- 38 43
- 39 44
- 295 300
- 295.04 300
Answer: B. 39 44
Explanation
Casting 295.04 to byte first gives 295, and 295 - 256 = 39 fits in a byte. Casting 300 to byte gives 300 - 256 = 44. The output is 39 44.
Step-by-step solution
- a = 295.04 is cast to byte: the double first becomes int 295, then 295 modulo 256 gives 39.
- b = 300 is cast to byte: 300 modulo 256 gives 44.
- The printed values are c and d, so the output is 39 44.
- This matches option B.
Why the other options are wrong
- A. The first value is 39, not 38, since 295 minus 256 is 39.
- C. No byte cast would give 295 and 300; a byte holds only -128 to 127.
- D. Casting to byte keeps only a value within -128 to 127, so the decimal values cannot print.
Tip: Casting to byte keeps only the low 8 bits: take the value modulo 256 and map it into -128 to 127.
Question 6Medium
Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
- 1 and 2
- 2 and 3
- 3 and 4
- All statements are correct.
Answer: D. All statements are correct.
Explanation
Statements (1), (2), (3), and (4) are correct.
- (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.
- (2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.
- (3) actually works, even though a cast is not necessary, because a long can store a byte.
Step-by-step solution
- Statement 1: casting 888.8 to int is legal and drops the decimal part.
- Statement 2: casting a long constant to byte is legal and keeps only the low 8 bits.
- Statement 3: a byte value can be stored in a long, so this is legal.
- Statement 4: casting a long to byte is legal, so this is also legal.
- All four are legal, so the correct answer is option D.
Why the other options are wrong
- A. Statements 3 and 4 are also legal, so this list is incomplete.
- B. Statements 1 and 4 are also legal, so this list is incomplete.
- C. Statements 1 and 2 are also legal, so this list is incomplete.
Tip: Narrowing casts are legal in Java; they simply drop the high bits.
Question 7Medium
What is the output of this program?
class mainclass {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
if (var1)
System.out.println(var1);
else
System.out.println(var2);
}
}
- 0
- 1
- true
- false
Answer: C. true
Explanation
The program checks if var1 is true, which it is, so it prints the value of var1, which is true. Therefore, the output is 'true'.
Step-by-step solution
- Var1 is set to true and var2 is set to false.
- if (var1) checks if var1 is true, which it is.
- So it prints the value of var1, which is true.
- The else block is skipped because var1 is true.
- Therefore, the output is 'true'.
Why the other options are wrong
- A. The program prints the boolean var1, which shows as true, not the number 0.
- B. Java prints boolean values as true or false, so the number 1 is never printed.
- D. false is var2, but the if branch runs because var1 is true, so var2 is never printed.
Tip: Remember that in Java, boolean variables are printed as 'true' or 'false'.
Question 8Medium
Which of these can not be used for a variable name in Java?
- identifier
- keyword
- Both a and b
- None of the mentioned
Answer: B. keyword
Explanation
Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc
Step-by-step solution
- Keywords are reserved words in Java and cannot be used as variable names.
- Examples of keywords include 'class', 'int', and 'for'.
- Identifier is a valid name for a variable in Java.
- Both a and b is wrong because an identifier can be used as a variable name.
- None of the mentioned is not a correct option since keyword is invalid.
- Final answer: B (keyword).
Why the other options are wrong
- A. An identifier is a valid variable name, so it can be used for a variable.
- C. An identifier can be used as a variable name, so both cannot be right; only a keyword is barred.
- D. None is wrong because a keyword cannot be used as a variable name.
Tip: Remember that keywords like 'class' or 'for' cannot be used as variable names in Java.
Question 9Medium
Which of these is data type long literal?
- 0x99fffL
- ABCDEFG
- 0x99fffa
- 99671246
Answer: A. 0x99fffL
Explanation
Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal.
Step-by-step solution
- Long literals end with 'L' or 'l'.
0x99fffL has an 'L' at the end, so it's a long literal.ABCDEFG is not a number, so it's not a long literal.0x99fffa lacks an 'L', so it's not a long literal.99671246 is a decimal number without 'L', so it's not a long literal.- Final answer: 0x99fffL.
Why the other options are wrong
- B. ABCDEFG is an identifier, not a number, so it cannot be a long literal.
- C. 0x99fffa is a hex int literal with no L suffix, so it is not a long literal.
- D. 99671246 is a decimal int literal with no L suffix, so Java treats it as int, not long.
Tip: Look for 'L' at the end of a number to identify a long literal.
Question 10Medium
What is the output of this program?
class c
{
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
}
}
- Hello c
- Hello
- Hello world
- Runtime Error.
Answer: D. Runtime Error.
Explanation
main is not declared static, so Java cannot start the program with it. The program fails at run time with a runtime error, and Hello is never printed.
Step-by-step solution
- The class has main as a non-static method, not public static void main(String[] args).
- Java needs a public static main method to start a program, so the launcher cannot start it.
- The launcher reports a run time error, so the answer is the runtime error option D.
Why the other options are wrong
- A. The program never prints a name, because main is never run as the entry point.
- B. The print line never runs, so Hello alone is never printed.
- C. The word world does not appear anywhere in the code.
Tip: Check that the entry point is public static void main(String[] args) before reading the output.
Practise all 44 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →