1.
What is the size of a Char?
Correct Answer
D. 16 bits
Explanation
A char data type in most programming languages is typically represented by 8 bits or 1 byte. However, in some languages like Java, a char is represented by 16 bits or 2 bytes. Therefore, the correct answer for the size of a char in this question is 16 bits.
2.
A class cannot be declared
Correct Answer
B. Private
Explanation
A class cannot be declared as private because the private access modifier restricts access to members within the same class only, not to the class itself. Private is used to hide members (such as variables and methods) from being accessed by other classes or objects. However, a class needs to be accessible by other classes in order to be used and instantiated. Therefore, declaring a class as private would render it inaccessible and unusable, making it impossible to create objects of that class.
3.
Following code will result in: int a = 3.5;
Correct Answer
A. Compilation error
Explanation
The code will result in a compilation error because it is trying to assign a floating-point value (3.5) to an integer variable (a). In Java, this type mismatch is not allowed and will cause a compilation error.
4.
Following code will result in: int a1 = 5; double a2 = (float)a1;
Correct Answer
B. No errors
Explanation
The code will not result in any errors because it is a valid type casting operation. The variable a1 is an integer with a value of 5, and it is being explicitly casted to a float before assigning it to the double variable a2. Since the float can be safely converted to a double without any loss of precision, there will be no errors during compilation or runtime.
5.
Following code will result in: int a = 9/0;
Correct Answer
B. Runtime Exception
Explanation
The code will result in a runtime exception because it tries to divide 9 by 0, which is not a valid operation in mathematics. This will cause a division by zero error at runtime, resulting in a runtime exception.
6.
Following code will result in: float a = 9/0
Correct Answer
B. Runtime Exception
Explanation
The code "float a = 9/0" will result in a runtime exception. This is because dividing a number by zero is not a valid mathematical operation and it throws a runtime exception called "DivideByZeroException".
7.
A class can be transient
Correct Answer
B. False
Explanation
A class cannot be transient. Transient is a keyword in programming languages, typically used to indicate that a variable should not be serialized, or in other words, should not be saved and restored when the object is written to a file or transferred over a network. However, the transient keyword cannot be applied to a class itself. It can only be used with variables within a class to specify that they should not be serialized. Therefore, the statement "A class can be transient" is incorrect.
8.
Following code will result in: class A { int b = 1; public static void main(String [] args) { System.out.println("b is " + b); }}
Correct Answer
A. Compilation error
Explanation
The given code will result in a compilation error because the variable "b" is an instance variable and cannot be accessed directly from a static method (main method). To access the instance variable, it needs to be accessed through an object of the class.
9.
Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}
Correct Answer
A. Compile error
Explanation
The given code will result in a compile error because class B extends class A, creating a cyclic inheritance relationship. In Java, cyclic inheritance is not allowed, and the compiler will throw an error. To resolve this issue, the inheritance relationship between class A and class B should be modified.
10.
Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}
Correct Answer
B. No errors
Explanation
The code will not result in any errors because class B extends class A, so it is valid to create an instance of B and assign it to a variable of type A. This is known as polymorphism in Java. However, it is important to note that at runtime, the actual object being referred to is of type B, so if any methods or fields specific to B are accessed using the variable 'a', it may result in a runtime exception.
11.
Methods that are marked protected can be called in any subclass of that class.
Correct Answer
A. True
Explanation
Protected methods in a class can be accessed by any subclass of that class. This means that if a method is marked as protected in a parent class, it can be called and used in any child class that inherits from the parent class. This allows for the child classes to have access to and utilize the protected methods defined in the parent class, providing flexibility and extensibility in the design and implementation of the class hierarchy.
12.
An abstract class can have non-abstract methods
Correct Answer
A. True
Explanation
An abstract class can have non-abstract methods because an abstract class is a class that cannot be instantiated and is meant to be inherited by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class have a complete implementation and can be called directly. This allows the abstract class to provide some default behavior that can be inherited by its subclasses.
13.
Java keywords are written in lowercase as well as uppercase
Correct Answer
A. True
Explanation
Java keywords are written in lowercase as well as uppercase. This means that Java keywords can be written in either lowercase or uppercase letters. The Java language is case-sensitive, so both forms of writing keywords are considered valid. However, it is a common convention to write Java keywords in lowercase letters to distinguish them from class names and variables, which are typically written in camel case. Therefore, the statement "Java keywords are written in lowercase as well as uppercase" is true.
14.
What is an instanceof
Correct Answer
B. An operator and keyword
Explanation
The correct answer is "An operator and keyword." The instanceof operator is used to check if an object is an instance of a particular class or interface. It returns true if the object is an instance of the specified class or interface, and false otherwise. It is a keyword in Java and is used in conjunction with the object and the class or interface being checked.
15.
Primitive datatypes are allocated on a stack
Correct Answer
A. True
Explanation
Primitive datatypes are allocated on a stack because they are simple and small in size. The stack is a region of memory that is used for local variables and function calls. When a primitive datatype is declared, its value is directly stored on the stack. This allows for efficient memory allocation and deallocation, as the stack automatically manages the memory for these variables. Additionally, since primitive datatypes are not objects, they do not require dynamic memory allocation on the heap like objects do.
16.
Can you compare a boolean to an integer?
Correct Answer
B. No
Explanation
A boolean represents a logical value, either true or false, while an integer represents a numeric value. Comparing a boolean to an integer would be comparing two different data types, which is not allowed in most programming languages. Therefore, it is not possible to directly compare a boolean to an integer.
17.
If class A implements an interface does it need to implement all methods of that interface?
Correct Answer
B. No, not when A is abstract
Explanation
When class A is declared as abstract, it means that it cannot be instantiated on its own. Abstract classes can have abstract methods, which are methods without an implementation. Therefore, when class A implements an interface, it is not required to implement all methods of that interface because the responsibility of implementing those methods can be deferred to its concrete subclasses.
18.
Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?
Correct Answer
B. FALSE
Explanation
When comparing the objects "a" and "b" using the "==" operator, it checks if the two objects refer to the exact same memory location. In this case, since "a" and "b" are both created using the "new" keyword, they are separate instances and occupy different memory locations. Therefore, the comparison will result in "FALSE" because "a" and "b" are not the same object.
19.
The methods wait(), notify() and notifyAll() in Object need to be called from synchronized pieces of code.
Correct Answer
A. True
Explanation
The methods wait(), notify(), and notifyAll() in the Object class need to be called from synchronized pieces of code because they are used for inter-thread communication and coordination. When a thread calls the wait() method, it releases the lock on the object and waits until another thread calls notify() or notifyAll() to wake it up. These methods should only be called when the calling thread owns the lock on the object, which is ensured by using synchronized blocks or methods. Calling these methods from unsynchronized code can lead to unexpected behavior and runtime errors.
20.
Inner classes can be defined within methods
Correct Answer
A. True
Explanation
Inner classes can be defined within methods. This means that a class can be declared inside another class's method. This allows for a more organized and modular code structure, as the inner class can have access to the variables and methods of the outer class. It is useful in scenarios where the inner class is only relevant to the method it is defined in and does not need to be accessed outside of it.
21.
Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.
Correct Answer
A. True
Explanation
The explanation for the given correct answer is that the "synchronized" keyword in Java is used to ensure that only one thread can access a particular block of code or method at a time. It provides a way to synchronize the execution of multiple threads and prevent potential conflicts or race conditions. By using the "synchronized" keyword, a thread must acquire the object lock before executing the synchronized block, ensuring that other threads cannot access the same block simultaneously. Therefore, the statement that "synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.
22.
The default statement of a switch is always executed
Correct Answer
B. False
Explanation
The default statement of a switch is not always executed. The default statement is only executed when none of the cases match the value being evaluated. If a case matches the value, the corresponding code block is executed and the switch statement terminates. If there is no default statement, then nothing is executed when none of the cases match.
23.
How can you prevent a member variable from becoming serialized?
Correct Answer
B. By marking it transient
Explanation
By marking a member variable as transient, you can prevent it from being serialized. When an object is serialized, all of its member variables are also serialized by default. However, by using the transient keyword, you can specify that a particular member variable should not be serialized. This is useful when the variable contains sensitive or unnecessary data that should not be included in the serialized object.
24.
What is Java (in regard to Computer Science) ?
Correct Answer
B. AN OBJECT-ORIENTED PROGRAMMING LANGUAGE
Explanation
Java is an object-oriented programming language. It is widely used in computer science for developing various applications and software. Java follows the principles of object-oriented programming, allowing developers to create modular and reusable code. It provides features like inheritance, encapsulation, and polymorphism, making it easier to manage and maintain large-scale projects. Java is platform-independent, meaning that it can run on different operating systems without the need for recompilation. It is known for its robustness, security, and scalability, making it a popular choice among developers.
25.
WHAT IS AN APPLET?
Correct Answer
A. A JAVA PROGRAM THAT IS RUN THROUGH A WEB BROWSER
Explanation
An applet is a small Java program that is designed to be run within a web browser. It is embedded within an HTML page and can be executed by the Java Virtual Machine (JVM) installed on the user's computer. Applets are commonly used to provide interactive features on websites, such as animations, games, or data visualization. They can be downloaded and executed by the web browser, allowing for dynamic and interactive content on web pages.
26.
Java runs on _______.
Correct Answer
B. All of the Above
Explanation
Java is a programming language that is designed to be platform-independent, meaning it can run on multiple operating systems. It is not limited to a specific operating system like Windows or Unix/Linux. Therefore, the correct answer is "All of the Above" as Java can run on Windows, Unix/Linux, and other operating systems as well.
27.
Why can't the whole program just consist of the one line that does the painting ?
Correct Answer
C. All of the above.
Explanation
The correct answer is "All of the above." This is because all three statements provided are valid explanations for why the whole program cannot just consist of the one line that does the painting. In Java, to create an applet, a class must be defined to call functions. Additionally, the drawString function is not defined without the "import" statements at the top. Therefore, all three statements contribute to the explanation of why the whole program cannot be reduced to just one line.
28.
What's the difference between an Applet and an application ?
Correct Answer
B. Applets are run over the web.
Explanation
Applets are small programs that are designed to be run within a web browser. They are typically used to provide interactive content on websites. On the other hand, applications are standalone programs that are installed on a computer and can be run independently of a web browser. Therefore, the correct answer is that applets are run over the web, while applications are not limited to running within a web browser.
29.
What is the main function of any variable ?
Correct Answer
B. To keep track of data in the memory of the computer
Explanation
The main function of any variable is to keep track of data in the memory of the computer. Variables are used to store and manipulate data during the execution of a program. They allow programmers to assign values to specific memory locations, which can then be accessed and modified as needed. By using variables, programmers can store and retrieve data, perform calculations, and make decisions based on the stored information. Variables play a crucial role in the functioning of computer programs by providing a way to store and manage data effectively.
30.
What is the proper way to declare a variable ?
Correct Answer
B. VariableType variableName;
Explanation
The proper way to declare a variable is by specifying the variable type followed by the variable name. This allows the compiler to allocate the appropriate amount of memory for the variable based on its type.
31.
Booleans are _______.
Correct Answer
A. True or False
Explanation
Booleans are a data type in programming that can have one of two values: true or false. They are used to represent logical values and are often used in conditional statements and comparisons. In this case, the correct answer is "True or False" because it accurately describes what booleans are - they can only have the values true or false.
32.
The following statements make “length” be what number ?
Correct Answer
A. 5
Explanation
The question is asking for the number that makes "length" equal to it. The given statements are "5" and "6". Since the answer is "5", it means that when "length" is equal to 5, the statements are true.
33.
What is an assignment statement ?
Correct Answer
B. Assigning a value to a variable
Explanation
An assignment statement refers to the action of assigning a value to a variable. It is a fundamental concept in programming where a specific value is assigned to a variable so that it can be stored and manipulated later in the program. This allows the program to remember and work with data throughout its execution.
34.
What will be the value of “num” after the following statements?
Correct Answer
B. 12
Explanation
The value of "num" will be 12 because the first statement assigns the value 1 to "num" and the second statement assigns the value 12 to "num". Therefore, the final value of "num" will be 12.
35.
If you want your conditional to depend on two conditions BOTH being true, what is the proper notation to put between the two Boolean statements ?
Correct Answer
A. &&
Explanation
The proper notation to put between two Boolean statements when you want your conditional to depend on both conditions being true is "&&". This is known as the logical AND operator. It returns true if both conditions are true and false otherwise.
36.
Which of the following means that in order for the conditional to happen, either x must be less than 3 or y must be greater than or equal to 4 ?
Correct Answer
A. if ((x < 3) || (y > = 4))
Explanation
The correct answer states that in order for the conditional to happen, either x must be less than 3 or y must be greater than or equal to 4. This means that if either of these conditions is true, the conditional statement will be executed. The use of the logical OR operator (||) indicates that only one of the conditions needs to be true for the overall condition to be true. Therefore, the correct answer is if ((x < 3) || (y > = 4)).
37.
What is a loop ?
Correct Answer
B. A segment of code to be run a specified amount of times
Explanation
A loop is a segment of code that is designed to be executed repeatedly for a specified number of times. It allows for efficient and repetitive execution of a block of code, eliminating the need to write the same code multiple times. By specifying the number of times the code should be executed, loops provide a way to automate repetitive tasks and make programs more efficient.
38.
What is essential in making sure that your loop is not infinite ?
Correct Answer
B. That your Boolean statement will at some point be false
Explanation
To ensure that a loop is not infinite, it is essential to have a Boolean statement in the code that will eventually become false. This allows the loop to terminate when the condition specified by the Boolean statement is no longer true. Without this condition, the loop would continue indefinitely, resulting in an infinite loop. Therefore, having a Boolean statement that will eventually be false is crucial for preventing infinite loops.
39.
Which is NOT a section of all types of loops ?
Correct Answer
B. The word "while"
Explanation
The word "while" is not a section of all types of loops. The "while" loop is a type of loop that executes a block of code repeatedly as long as a specified condition is true. However, there are other types of loops such as "for" and "do-while" loops that do not include the "while" keyword. Therefore, the word "while" is not a section of all types of loops.
40.
In a ‘for’ loop, what section of the loop is not included in the parentheses after “for” ?
Correct Answer
A. Loop Body
Explanation
In a 'for' loop, the loop body is not included in the parentheses after 'for'. The loop body is the section of the code that is executed repeatedly until the loop condition becomes false. The initialization section, on the other hand, is included in the parentheses after 'for' and is used to initialize the loop counter or any other necessary variables before the loop starts executing.
41.
What is a function in terms of Computer Science ?
Correct Answer
A. A group of code lines that performs a specific task
Explanation
A function in computer science refers to a group of code lines that are written to perform a specific task. It is a way to organize and encapsulate a set of instructions that can be reused multiple times throughout a program. Functions help in making the code more modular, readable, and efficient by breaking it down into smaller, manageable tasks. They can take input parameters, perform computations, and return output values, allowing for better code organization and reusability.
42.
What does AWT stands for ?
Correct Answer
A. Abstract window Toolkit
Explanation
AWT stands for Abstract Window Toolkit. It is a set of application programming interfaces (APIs) provided by Java for creating graphical user interfaces (GUIs) for Java programs. AWT provides a collection of components such as buttons, labels, text fields, etc., that can be used to build GUI applications. It also includes classes for handling events, layout managers for arranging components, and graphics classes for drawing on the screen. AWT is platform-independent and can be used to create GUIs that run on different operating systems.