1.
All Java source files must end with the extension .class
Correct Answer
B. False
Explanation
Java source files do not need to end with the extension ".class". The ".class" extension is used for compiled Java bytecode files, not for the source code files. Java source files typically have the extension ".java".
2.
A source file may contain an unlimited number of non-public class definitions.
Correct Answer
A. True
Explanation
A source file in programming can indeed contain an unlimited number of non-public class definitions. This means that multiple classes can be defined within a single source file, as long as they are not declared as public. This allows for better organization and encapsulation of code within a program, as related classes can be grouped together in the same file without exposing them to other parts of the program.
3.
If a source file includes a public class, the class name must match the unextended filename.
Correct Answer
A. True
Explanation
In Java, when a source file includes a public class, the class name must match the unextended filename. This means that the name of the public class in the source file must be the same as the name of the file (excluding the file extension). This is a requirement imposed by the Java language to ensure consistency and ease of use. If the class name does not match the filename, it will result in a compilation error. Therefore, the given answer "True" is correct.
4.
Each source file that you compile will produce one file with an extension .class
Correct Answer
B. False
Explanation
Each source file that you compile will produce one file with an extension .java, not .class. The .class extension is used for compiled bytecode files in Java.
5.
At least one of the following three top-level elements must appear in every source file. If they are included in the source file, they must appear in the following order.A. package declaration B. import declarations C. class definitions
Correct Answer
B. False
Explanation
The statement is false because it states that at least one of the three top-level elements must appear in every source file. However, it is possible for a source file to not have any of these elements.
6.
The compiler does not require you to use import directives.
Correct Answer
A. True
Explanation
In some programming languages, import directives are used to include external libraries or modules in the code. However, there are cases where the compiler automatically includes commonly used libraries without requiring explicit import directives. This means that in those cases, the compiler does not require the programmer to use import directives. Therefore, the statement "The compiler does not require you to use import directives" is true.
7.
What output is produced by the following program?import java.awt.*;
public class Q001_07{
public static void main(String args[]){
Button aButton = new Button("Button");
Label aLabel = new Label("Label");
System.out.println("OK");
}
}
Correct Answer
C. OK
Explanation
The given program creates a Button object and a Label object. It then prints "OK" to the console. Therefore, the output produced by the program is "OK".
8.
As in C and C++, goto and const are keywords that are actively used in Java.
Correct Answer
B. False
9.
Which of the following characters may be used as the first character in a Java identifier?
Correct Answer(s)
A. X (the letter x)
C. $ (the dollar sign character)
D. _ (the underscore character)
Explanation
In Java, an identifier is a name used to identify a variable, method, class, or other programming element. The first character of an identifier must be a letter, a dollar sign ($), or an underscore (_). Therefore, the characters x, $, and _ can be used as the first character in a Java identifier. The number 4 cannot be used as the first character in a Java identifier, as it does not meet the requirement of starting with a letter, $, or _.
10.
What output is produced by compiling and running the following program? Note that the instance variable named x is declared private(as highlighted in boldface in the code).class Q001_10{
public static void main(
String args[]){
AClass ref1 = new AClass(5);
AClass ref2 = new AClass(10);
System.out.println(
ref1.add(ref2));
}//end main()
}//end class definition
class AClass{
private int x;
AClass(int x){//constructor
this.x = x;
}// end constructor
int add(AClass ref){
return x + ref.x;
}//end add()
}
Correct Answer
C. 15
Explanation
The program will output 15. This is because the program creates two instances of the AClass object, ref1 and ref2, with x values of 5 and 10 respectively. The add method in AClass takes another AClass object as a parameter and adds the x values of the two objects together. In this case, it adds 5 (from ref1) to 10 (from ref2), resulting in 15. The program then prints out this value using System.out.println.
11.
Which of the following are the possible states of a boolean variable?
Correct Answer(s)
A. True
C. False
Explanation
A boolean variable can have two possible states: true or false. In programming, a boolean variable is used to represent logical values, where true represents a condition that is considered to be true, and false represents a condition that is considered to be false. Therefore, the correct answer is true and false.
12.
All of the integral types, including char are signed.
Correct Answer
B. False
Explanation
The statement is false because not all integral types, including char, are signed. In C++, the char type can be either signed or unsigned, depending on the implementation. By default, char is signed on some systems, while on others it is unsigned. Therefore, it is incorrect to say that all integral types, including char, are signed.
13.
Characters in Java are represented by the 8-bit Extended ASCII character set.
Correct Answer
B. False
Explanation
In Java, characters are actually represented by the Unicode character set, not the 8-bit Extended ASCII character set. Unicode allows for a much wider range of characters to be represented, including characters from different languages and special symbols. Therefore, the given statement is false.
14.
Which, if any, of the following are not valid literal values for type char?
Correct Answer
D. 'end'
Explanation
The correct answer is 'end'. In the given options, '\u1632' is a valid Unicode escape sequence representing a character, '\t' is a valid escape sequence representing a tab character, and "a" is a valid string literal. However, 'end' is not a valid literal value for type char as it consists of multiple characters.
15.
What output is produced by the following program?class Q002_10{
public static void main(String args[]){
int x = 1;
int y = ~x + 1;
System.out.println(y);
}//end main()
}
Correct Answer
B. -1
Explanation
The program initializes the variable x with the value 1. The variable y is then assigned the value of the bitwise complement of x plus 1. The bitwise complement of 1 is -2, and adding 1 to -2 gives -1. Therefore, the output of the program is -1.
16.
By default, an integral literal is a 64 bit value.
Correct Answer
B. False
Explanation
An integral literal is not always a 64-bit value by default. The size of an integral literal depends on the programming language and the platform being used. For example, in C++, the size of an integral literal can vary depending on the suffix used (such as 'L' for long, 'LL' for long long, etc.). Therefore, the statement "By default, an integral literal is a 64-bit value" is false.
17.
The default for a floating-point literal without an F or a D is a 32-bit float.
Correct Answer
B. False
Explanation
The default for a floating-point literal without an F or a D is not a 32-bit float. The default is actually a 64-bit double.
18.
All elements in a Java array must be of the same type.
Correct Answer
A. True
Explanation
In Java, arrays are used to store multiple elements of the same data type. When creating an array, we need to specify the data type of its elements. This means that all elements in a Java array must be of the same type. If we try to mix different data types in an array, it will result in a compilation error. Therefore, the statement "All elements in a Java array must be of the same type" is true.
19.
The size of a Java array can be specified using a variable or a literal.
Correct Answer
A. True
Explanation
In Java, the size of an array can indeed be specified using either a variable or a literal. This means that you can either use a specific number to define the size of the array or use a variable that holds the desired size value. This flexibility allows for dynamic sizing of arrays based on different conditions or data inputs.
20.
Consider the following simple program, which initializes the instance variable named myIntVar to a value of 10 when the instance variable is initialized.What value will be displayed by this program?class Q56{
int myIntVar = 10;//instance variable
Q56(){//constructor
myIntVar = 20;
}//end constructor
public static void main(String args[]){
System.out.println(new Q56().myIntVar);
}//end main()
}
Correct Answer
C. 20
Explanation
The program initializes the instance variable myIntVar to a value of 10 when it is declared. However, in the constructor, the value of myIntVar is changed to 20. Therefore, when the program is run, it will display the value of myIntVar, which is 20.