1.
Following code will result in: int a = 3.5;
Correct Answer
A. Compilation error
Explanation
The given 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 is not allowed without explicit type casting.
2.
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 value of the integer variable a1 is being explicitly casted to a float type and then assigned to the double variable a2. Since the float type can be safely promoted to a double type without any loss of precision, the code will execute without any errors.
3.
Following code will result in: int a = 9/0;
Correct Answer
B. Runtime Exception
Explanation
The given code will result in a runtime exception because it is trying to divide 9 by 0, which is not a valid operation in mathematics. This will throw an ArithmeticException at runtime.
4.
Following code will result in: float a = 9/0
Correct Answer
B. Runtime Exception
Explanation
The given code will result in a runtime exception because it attempts to divide the number 9 by 0. Division by zero is not defined in mathematics and is considered an error in programming. When this code is executed, it will throw a runtime exception specifically for dividing by zero, causing the program to terminate abruptly.
5.
A class can be transient
Correct Answer
B. False
Explanation
A class cannot be transient. Transient is a keyword in programming languages like Java that is used to indicate that a variable should not be serialized, meaning it should not be saved or restored when the object is serialized or deserialized. However, the transient keyword cannot be applied to a class itself. It can only be applied to variables within a class. Therefore, the statement "A class can be transient" is false.
6.
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 declared as an instance variable within the class A, but it is being accessed in the static main method. Since the main method is a static method, it does not have access to instance variables directly. To access the variable "b" in the main method, it either needs to be declared as a static variable or an instance of class A needs to be created to access the instance variable.
7.
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 code will result in a compile error because class B extends class A, but class A does not have a constructor that takes no arguments. Therefore, when trying to create a new instance of class B in the main method of class A, the compiler will throw an error because it cannot find a suitable constructor in class A to instantiate class B.
8.
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 assign an instance of class B to a variable of type A. The code will compile and run without any issues.
9.
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 superclass, it can be called and used in any subclass that extends that superclass. This allows for inheritance and the ability to override or modify the behavior of the protected method in the subclass. Therefore, the statement "Methods that are marked protected can be called in any subclass of that class" is true.
10.
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 extended by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class can have a complete implementation and can be directly called by the subclasses that inherit from the abstract class. This allows the abstract class to provide common functionality to its subclasses while still allowing the subclasses to implement their own unique functionality.
11.
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 statement is true because Java is case-sensitive, meaning that lowercase and uppercase letters are treated as distinct characters. Keywords in Java, such as "public," "class," and "static," are predefined and have specific meanings in the language. These keywords must be written in lowercase letters to be recognized by the Java compiler. However, Java also allows developers to use uppercase letters for keywords, although it is not a common practice.
12.
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 whether an object belongs to a particular class or implements a particular interface. It returns true if the object is an instance of the specified class or interface, and false otherwise. Therefore, instanceof is both an operator and a keyword in Java.
13.
Primitive datatypes are allocated on a stack
Correct Answer
A. True
Explanation
Primitive datatypes are allocated on a stack. This means that when a primitive datatype is declared, memory is allocated for it on the stack. The stack is a region of memory that is used for temporary storage of variables. When a variable goes out of scope, the memory allocated for it on the stack is automatically released. This is different from objects, which are allocated on the heap and require manual memory management.
14.
Can you compare a boolean to an integer?
Correct Answer
B. No
Explanation
A boolean is a data type that represents two possible values: true or false. An integer, on the other hand, is a data type that represents whole numbers. These two data types are not compatible for direct comparison because they represent different concepts. In most programming languages, attempting to compare a boolean to an integer would result in a type mismatch error. Therefore, the correct answer is "No".
15.
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 a class A implements an interface, it is generally required to implement all the methods of that interface. However, if class A is declared as abstract, it is not mandatory to implement all the methods of the interface. This is because abstract classes can have abstract methods, which do not require implementation in the abstract class itself. The responsibility of implementing these abstract methods is passed on to the concrete subclasses of the abstract class. Therefore, when class A is abstract, it is not necessary to implement all the methods of the interface in class A.
16.
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 two Integer objects using the "==" operator, it checks if the two objects refer to the same memory location. In this case, even though the values of a and b are both 2, they are separate objects created using the "new" keyword. Therefore, they will have different memory locations, resulting in the comparison returning false.
17.
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 are used for inter-thread communication and synchronization. They are designed to be called from synchronized blocks or methods. When a thread calls wait(), it releases the lock it holds and enters a waiting state until another thread notifies it. Similarly, notify() and notifyAll() are used to wake up threads that are waiting. To ensure proper synchronization and avoid race conditions, these methods should only be called within synchronized code blocks or methods. Therefore, the statement is true.
18.
Inner classes can be defined within methods
Correct Answer
A. True
Explanation
Inner classes can be defined within methods in Java. An inner class is a class that is defined inside another class. It can be defined within a method, allowing it to have access to the variables and methods of the enclosing method. This can be useful in situations where a class is only needed within a specific method and does not need to be accessed outside of it. By defining the inner class within the method, it keeps the code more organized and encapsulated.
19.
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 block of code or an object at a time. When a thread encounters a synchronized block or method, it grabs the object lock associated with that block or method and other threads have to wait until the lock is released. This helps in preventing multiple threads from accessing and modifying shared data simultaneously, thereby ensuring thread safety and avoiding race conditions. Therefore, the statement that "synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.
20.
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 tested. If a case matches the value, the corresponding statement is executed and the program exits the switch statement. If there is no default statement, the program simply continues to the next line of code after the switch statement. Therefore, the correct answer is False.
21.
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, it indicates to the serialization process that this variable should not be serialized. This is useful when there are certain variables that should not be saved or transmitted when an object is being serialized. The transient keyword allows for control over which variables are included in the serialization process, preventing the member variable from being serialized. The volatile keyword, on the other hand, is used for ensuring that the value of a variable is always read from the main memory and not from a cache, and it does not have any direct relation to serialization.
22.
What is Java (in regard to Computer Science) ?
Correct Answer
B. AN OBJECT-ORIENTED PROGRAMMING LANGUAGE
Explanation
Java is an object-oriented programming language widely used in computer science. It was developed by Sun Microsystems and is known for its platform independence, meaning that Java programs can run on any device that has a Java Virtual Machine. Java is popular for its simplicity, robustness, and security features. It is used to develop a wide range of applications, including web and mobile applications, enterprise software, and embedded systems. Java's object-oriented nature allows for the creation of reusable code and promotes modular and scalable software development.
23.
WHAT IS AN APPLET?
Correct Answer
A. A JAVA PROGRAM THAT IS RUN THROUGH A WEB BROWSER
Explanation
An applet is a Java program that is designed to be executed within a web browser. It is a small application that is typically embedded in a web page and runs on the client-side. Applets are used to provide dynamic and interactive content on websites. They can perform various tasks such as animations, data processing, and user input handling. By running within a web browser, applets can be easily accessed and executed by users without the need for additional installations or configurations.
24.
Java runs on _______.
Correct Answer
B. All of the Above
Explanation
Java is a platform-independent programming language, which means it can run on multiple operating systems including Windows, Unix/Linux, and others. Therefore, the correct answer is "All of the Above" as Java can run on all the mentioned operating systems.
25.
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, you need to define a class 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 just consist of the one line that does the painting.
26.
What's the difference between an Applet and an application ?
Correct Answer
B. Â Applets are run over the web.
Explanation
An applet is a small program that is designed to be run within another application or through a web browser. It is typically used to enhance the functionality of a website. On the other hand, an application is a standalone program that is installed and run directly on a computer or device. The key difference between an applet and an application is that applets are specifically designed to be run over the web, while applications are not limited to web-based environments. Therefore, the correct answer is that applets are run over the web.
27.
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 in computer programs. They provide a way to assign values to a specific location in memory, which can then be accessed and modified throughout the program. By using variables, programmers can store and retrieve data as needed, making it an essential aspect of programming.
28.
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.
29.
Booleans are _______.
Correct Answer
A. Â True or False
Explanation
Booleans are data types that can have one of two values: true or false. They are used to represent logical values and are commonly used in programming to make decisions and control the flow of a program.
30.
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 options are 5 and 6. Since the answer is 5, it means that "length" is equal to 5.
31.
What is an assignment statement ?
Correct Answer
B. Assigning a value to a variable
Explanation
An assignment statement is a statement that assigns a value to a variable. It is used to store a value in a variable so that it can be used later in the program. By assigning a value to a variable, we are giving it a specific data value that can be manipulated or used in calculations or comparisons. This allows us to store and manipulate data in a program, making it an essential concept in programming.
32.
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 given statements are "1" and "12", which means that the value of "num" is being assigned as 12 in the second statement.
33.
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 the logical operator for "and" in many programming languages. It evaluates to true only if both conditions are true, otherwise, it evaluates to false.
34.
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 is "if ((x < 3) || (y >= 4))". This means that for the conditional statement to be true, either x must be less than 3 or y must be greater than or equal to 4.
35.
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 repeated a specified number of times. It allows for the execution of a certain block of code repeatedly until a specific condition is met. Loops are used to automate repetitive tasks and make the code more efficient by reducing the need for duplicate code. They are an essential part of programming and are commonly used in various programming languages to iterate through arrays, perform calculations, and execute a specific set of instructions multiple times.
36.
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
In order to ensure that a loop is not infinite, it is essential to have a Boolean statement somewhere in the code that will eventually be false. This allows the loop to terminate and prevents it from running indefinitely. Without a condition that can evaluate to false, the loop will continue to repeat indefinitely, resulting in an infinite loop.
37.
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. While loops have a test condition that is evaluated before each iteration, and if the condition is true, the loop body is executed. Other types of loops, such as for loops and do-while loops, have different sections, such as initialization, increment/decrement, and loop body. Therefore, the word "while" does not apply to all types of loops.
38.
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 section of the loop that is not included in the parentheses after 'for' is the Loop Body. The loop body contains the code that is executed repeatedly until the loop condition is false. The initialization section, on the other hand, is included in the parentheses after 'for' and is responsible for initializing the loop control variable.
39.
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 is a group of code lines that performs a specific task. It is a way to organize and modularize code by breaking it down into smaller, reusable units. Functions can be called and executed multiple times throughout a program, making it easier to maintain and debug code. They can also accept input parameters and return output values, allowing for flexibility and reusability in programming.
40.
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 platform-independent way of creating windows, buttons, menus, and other GUI components. It is a part of the Java Foundation Classes (JFC) and is used to develop GUI-based applications in Java.
41.
11.1 Q1: Which of the following statements is false?
Correct Answer
B. B. Exception handling can catch but not resolve exceptions.
Explanation
Exception handling enables programmers to write robust and fault-tolerant programs by providing a mechanism to catch and handle exceptions. It allows programmers to anticipate and handle potential errors or exceptional situations that may occur during program execution. However, exception handling alone cannot resolve exceptions, as resolving exceptions typically involves taking appropriate actions to correct the error or exceptional situation that caused the exception. Therefore, statement b is false.
42.
11.2 Q1: Intermixing program and error-handling logic:
Correct Answer
C. C. can degrade a program’s performance, because the program must perform (potentially frequent) tests to determine whether a task executed correctly and the next task can be performed.
Explanation
Intermixing program and error-handling logic can degrade a program's performance because it requires the program to perform tests to determine if a task executed correctly and if the next task can be performed. This can result in potentially frequent tests, which can slow down the program's execution. Therefore, option c is the correct answer as it accurately describes the impact of intermixing program and error-handling logic on a program's performance.
43.
11.3 Q2: Which of the following is not included in an exception’s stack trace?
Correct Answer
D. D. Instructions on handling the exception.
Explanation
The stack trace of an exception typically includes information about the method-call stack at the time the exception occurred, the name of the exception, and a descriptive message for the exception. However, it does not include instructions on handling the exception. The stack trace is mainly used for debugging purposes and helps identify the sequence of method calls that led to the exception. Instructions on handling the exception are typically provided in the code where the exception is caught and handled.
44.
11.3 Q3: Which of the following statements regarding the throw point of an exception is false?
Correct Answer
A. A. It specifies the point at which the exception must be handled.
Explanation
The throw point of an exception refers to the initial point at which the exception occurs, not the point at which it must be handled. The throw point is the location in the code where the exception is thrown, and it is up to the programmer to decide where and how to handle the exception. Therefore, statement a is false.
45.
Which is the valid declarations within an interface definition?
Correct Answer
A. Public double methoda();
Explanation
Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.
Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.
Option C is wrong. static is concerned with the class and not an instance.
46.
Which is right way to creating an array of integer in java?
Correct Answer
C. Int[] javaArray = new int[10];
Explanation
The correct way to create an array of integers in Java is by using the syntax "int[] javaArray = new int[10];". This creates an array named "javaArray" with a length of 10, capable of storing integer values. The other options provided in the question are incorrect. "int javaArray = new int[10];" is incorrect because it declares a single integer variable instead of an array. "int avaArray new = myArray[ ];" is incorrect because it contains syntax errors and does not create an array. "none" is also incorrect because there is a correct way to create an array in Java.
47.
Which keyword is used to inherit class?
Correct Answer
B. Extends
Explanation
The keyword "extends" is used to inherit a class in object-oriented programming. When a class extends another class, it means that the subclass inherits all the properties and methods of the superclass. This allows the subclass to reuse code from the superclass and also add its own unique functionality. Therefore, "extends" is the correct keyword to indicate inheritance in Java and other similar languages.
48.
Which of the java classes that cannot be subclassed?
Correct Answer
C. Final class
Explanation
A final class in Java cannot be subclassed because it is marked with the "final" keyword, which indicates that it cannot be extended. This is often done to prevent further modification or extension of the class, ensuring that its behavior remains consistent and preventing any unintended changes that may break its functionality. Therefore, a final class is considered to be a complete and final implementation that cannot be altered or extended by any other class.
49.
An abstract class should have methods all declared abstract?
Correct Answer
B. No
Explanation
An abstract class can have both abstract and non-abstract methods. It is not necessary for all methods in an abstract class to be declared as abstract. Abstract methods are those that do not have any implementation and must be overridden by the subclasses. Non-abstract methods in an abstract class can have their own implementation and can be used by the subclasses without any modification. Therefore, an abstract class should not have all methods declared as abstract.
50.
The finalize() method present in Object class is called just prior to
Correct Answer
D. Before garbage collection
Explanation
The finalize() method present in the Object class is called before garbage collection. Garbage collection is the process of automatically reclaiming memory that is no longer in use by objects in a program. When an object or variable goes out of scope, meaning it is no longer accessible or needed, it becomes eligible for garbage collection. The finalize() method allows an object to perform any necessary cleanup operations before it is garbage collected. Therefore, the correct answer is "before garbage collection."