1.
Which of the following lines will compile without warning or error.
Correct Answer
E. int i=10;
Explanation
The line "int i=10;" will compile without warning or error because it declares an integer variable "i" and assigns it the value of 10, which is a valid operation in Java. The other lines will not compile without warning or error because they have type mismatches or invalid assignments.
2.
A byte can be of what size
Correct Answer
A. 8 bits
Explanation
A byte is a unit of digital information that consists of 8 bits. Each bit can represent a binary value of either 0 or 1. Therefore, a byte can store 8 binary digits, allowing for a total of 256 different possible values.
3.
Which of the following are not keywords or reserved words in Java?
Correct Answer
B. Then
Explanation
The keyword "then" is not a keyword or reserved word in Java. Java does not have a "then" keyword. It is commonly used in other programming languages like Pascal and BASIC, but not in Java.
4.
String is a primitive datatype
Correct Answer
B. False
Explanation
The given statement is false because string is not a primitive datatype. In most programming languages, string is considered as a complex or composite datatype, as it is a sequence of characters. Primitive datatypes typically include integers, floating-point numbers, booleans, and characters, but not strings.
5.
Which statements about the output of the following program are true?
public class EqualTest {
public static void main(String args[]) {
String s1 = “YES”;
String s2 = “YES”;
if ( s1 == s2 ) System.out.println(“equal”);
String s3 = new String(“YES”);
String s4 = new String(“YES”);
if ( s3 == s4 ) System.out.println(“s3 eq s4”);
}
}
Correct Answer
B. “equal” is printed only.
Explanation
The reason why "equal" is printed only is because when the strings s1 and s2 are declared and assigned the value "YES", they are both pointing to the same string object in the string pool. Therefore, when the if statement checks if s1 is equal to s2 using the == operator, it returns true and "equal" is printed. However, when s3 and s4 are declared and assigned the value "YES" using the new keyword, they are creating two separate string objects in the heap memory. Therefore, when the if statement checks if s3 is equal to s4 using the == operator, it returns false and "s3 eq s4" is not printed.
6.
Which of the following is true about && operator?
Correct Answer
D. The && operator does not evaluate its second operand if the value of the first operand is false.
Explanation
The correct answer is that the && operator does not evaluate its second operand if the value of the first operand is false. This means that if the first operand is false, the second operand is not checked and the overall result of the expression will be false regardless of the value of the second operand. This behavior is known as short-circuiting and can be useful in situations where evaluating the second operand may have side effects or be computationally expensive.
7.
What value is assigned to x as the result of executing the following code?
int y = 3;
y++;
x = y++;
Correct Answer
B. 4
Explanation
The value assigned to x is 4 because the code first assigns the value of y (which is 3) to x. Then, the y variable is incremented by 1 using the y++ statement. So, the final value of y becomes 4, and that is the value assigned to x.
8.
What is the difference between >> and >>>?
Correct Answer
C. The >> operator fills the shifted out high order bits based on the sign bit and the >>> operator fills the high order bits with zeros.
Explanation
The >> operator fills the shifted out high order bits based on the sign bit, meaning that if the sign bit is 1 (indicating a negative number), the high order bits will be filled with 1s. On the other hand, the >>> operator fills the high order bits with zeros, regardless of the sign bit.
9.
Which of the following is a legal operation?
Correct Answer
A. S3=s1 + s2;
Explanation
The correct answer is s3=s1 + s2;. This is a legal operation because it is performing addition on two variables, s1 and s2, and assigning the result to s3.
10.
Choose the options that are true about features of instance variable. Choose any 2
Correct Answer(s)
B. Accessible anywhere in the class
C. Static variables are initialized at class load time
Explanation
Instance variables are accessible anywhere in the class, meaning they can be accessed and used by any method or constructor within the class. Static variables, on the other hand, are initialized at class load time, which means they are initialized before any constructor is invoked. Therefore, the correct options are "Accessible anywhere in the class" and "Static variables are initialized at class load time".
11.
Which of the following will output -4.0
Correct Answer
C. System.out.println(Math.ceil(-4.7));
Explanation
The correct answer is System.out.println(Math.ceil(-4.7)). This is because the Math.ceil() function returns the smallest integer greater than or equal to a given number. In this case, -4.7 is rounded up to -4.0.
12.
.In case of Logical AND, if exp1 is false, then the operator never evaluates exp2. True or False
Correct Answer
A. True
Explanation
In the case of Logical AND, if exp1 is false, then the operator never evaluates exp2. This means that if the first expression is false, the second expression is not checked or evaluated. Therefore, the statement is true.
13.
Choose which is not a characteristic of instanceof operator
Correct Answer
B. The instanceof operator can be used with primitive types.
Explanation
The instanceof operator cannot be used with primitive types. It can only be used with object references.
14.
The only data that cannot be typecasted is
Correct Answer
C. Boolean
Explanation
The boolean data type in programming represents true or false values. It is a primitive data type and cannot be typecasted into any other data type. Typecasting is the process of converting one data type into another. However, boolean values cannot be converted into any other data type because they only have two possible values. Therefore, the correct answer is boolean.
15.
When you convert a data that has a large type to a smaller type, you must use an explicit cast. True or false
Correct Answer
A. True
Explanation
When converting a data that has a large type to a smaller type, an explicit cast is required. This is because the smaller type may not be able to accommodate the full range of values that the larger type can hold. By using an explicit cast, you are indicating to the compiler that you are aware of the potential loss of data and are intentionally truncating or rounding the value to fit within the smaller type. Therefore, the correct answer is true.
16.
If you try to access a local variable that is not initialized then
Correct Answer
B. It throws a compile time error
Explanation
When you try to access a local variable that is not initialized, it throws a compile-time error. This means that the compiler detects that the variable has not been assigned a value before it is being used, and it raises an error to prevent any potential issues or unexpected behavior in the program.
17.
Byte and short, cannot be converted to char or the vice versa
Correct Answer
A. True
Explanation
In Java, byte and short are smaller data types compared to char. When converting from byte or short to char, there is a possibility of losing information because char can hold a wider range of values. Therefore, such conversions are not allowed by the Java language. Similarly, when converting from char to byte or short, there is a possibility of losing information as well. Hence, the statement that byte and short cannot be converted to char or vice versa is true.
18.
Is null a keyword in java.
Correct Answer
B. False
Explanation
The correct answer is False. "null" is not a keyword in Java. It is a literal that represents the absence of a value or a null reference. Keywords in Java are reserved words that have a specific meaning and cannot be used as identifiers.
19.
Which of the following is not a variable name?
Correct Answer(s)
B. Null
C. _sum
D. 2sqrt
E. Count number
Explanation
The variable name "Null" is not a valid variable name because it is a reserved keyword in many programming languages and cannot be used as a variable name. Similarly, the variable name "2sqrt" is not a valid variable name because it starts with a number, which is not allowed in most programming languages. The variable name "_sum" is a valid variable name because it starts with an underscore and is followed by letters or numbers. The variable name "count number" is not a valid variable name because it contains a space, which is not allowed in most programming languages.
20.
Which of the following are legal identifiers. Choose all that apply
Correct Answer(s)
B. Variable2
C. _whatavariable
D. _3_
E. $anothervar
Explanation
The question asks for legal identifiers, which are names used to identify variables, functions, or other entities in programming. In most programming languages, identifiers must follow certain rules. They can start with a letter or an underscore, and the rest of the characters can be letters, numbers, or underscores. They cannot start with a number or contain special characters like # or $. Therefore, the legal identifiers in this question are variable2, _whatavariable, _3_, and $anothervar.
21.
Which one of the following is not a token?
Correct Answer
B. Method
Explanation
A method is not considered a token because tokens are the smallest individual units of a program, and a method is a collection of statements and code that performs a specific task. Tokens include keywords, literals, and identifiers, which are all individual components that make up a program.
22.
Local variables are not automatically initialized. State True or False
Correct Answer
A. True
Explanation
Local variables in programming languages are not automatically initialized with default values. This means that when a local variable is declared, it does not have a predefined value and will contain whatever value is stored in that memory location at that time. Therefore, it is true that local variables are not automatically initialized.
23.
The size of a long integer data type is:
Correct Answer
D. 64 bits
Explanation
A long integer data type typically refers to a data type that can store larger integer values than a regular integer data type. In most programming languages, a long integer is usually 64 bits in size. This means that it can store integer values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Therefore, the correct answer is 64 bits.
24.
Given the following variables which of the following lines will compile without error?
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
Choose 2
Correct Answer(s)
B. J=i
D. J=i
Explanation
The given code snippet initializes variables s, l, d, i, and j with their respective values. The line "j=i" assigns the value of variable i to variable j, which is of type int. Since both variables are of the same type, the assignment will compile without error. Therefore, the lines "j=i" and "j=i" will compile without error.
25.
Which one of the following is not true in the case of Type declaration statement?
Correct Answer
B. Different types of variables can be declared in a single Type declaration statement
Explanation
In a Type declaration statement, different types of variables cannot be declared together. Each variable must have the same data type. Therefore, the statement "Different types of variables can be declared in a single Type declaration statement" is not true.
26.
Extended assignment operators (for example +=) do an implicit cast.
byte b = 10;
b += 10;
Correct Answer
A. True
Explanation
When using extended assignment operators, such as +=, the value on the right side of the operator is implicitly cast to the type of the variable on the left side. In this case, the variable b is of type byte, and the value 10 on the right side is an int. However, since the += operator does an implicit cast, the value 10 is automatically cast to a byte before being added to b. Therefore, the statement b += 10; is valid and will not result in a compilation error. Hence, the correct answer is True.
27.
State whether True or false. The default value for a boolean member variable is true.
Correct Answer
B. False
Explanation
The default value for a boolean member variable is false, not true. When a boolean member variable is declared but not assigned a value, it automatically defaults to false.
28.
The character pair ?: is called the relational operator.
Correct Answer
B. False
29.
Which of the following has highest operator precedence
Correct Answer
A. ++
Explanation
The operator "++" has the highest precedence among the given options. This means that it is evaluated first before any other operators in an expression. The "++" operator is used to increment the value of a variable by 1.
30.
On what situations can you use the ‘==‘ operator?
Correct Answer(s)
A. It can be used instead of equals()
B. To compare two primitives, use the == operator.
C. To see if two references are the same,
Explanation
The '==' operator can be used instead of the equals() method to compare two objects for equality. It can also be used to compare two primitive data types for equality. Additionally, the '==' operator can be used to check if two references are referring to the same object in memory. Lastly, the '==' operator can be used to assign a value to a variable.