1.
If none of the private/protected/public is specified for a member, that member ...
Correct Answer
B. Is only accessible by other classes of the same package
Explanation
If none of the access modifiers (private, protected, public) is specified for a member, that member is only accessible by other classes of the same package. This means that the member can be accessed by any other class within the same package, but not by classes in different packages.
2.
Which one is not correct?
Correct Answer
C. Class and object are just different names for the same thing
Explanation
The statement "Class and object are just different names for the same thing" is not correct. In object-oriented programming, a class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the properties and behaviors that an object will have, and multiple objects can be created from the same class. Therefore, a class and an object are not the same thing, but rather, a class is used to create objects.
3.
Which is not a part of defining an object?
Correct Answer
A. Description
Explanation
An object in programming is typically defined by its name, methods, and associations with other objects. The description, on the other hand, is not a necessary component for defining an object. While a description can provide additional information about the object, it is not essential for its definition. The name is used to identify the object, methods define its behavior or actions, and associations establish relationships with other objects.
4.
Class B inherits from Class A, what cannot be said:
Correct Answer
C. B has access to private members of A
Explanation
Since Class B inherits from Class A, it can access the protected and public members of Class A. However, it cannot access the private members of Class A. Private members are only accessible within the same class and not by any derived classes. Therefore, it cannot be said that B has access to the private members of A.
5.
What is a member of a class
Correct Answer
C. Attribute or method
Explanation
A member of a class refers to any attribute or method that belongs to that class. It can be either an attribute, which represents the data or state of the object, or a method, which represents the behavior or actions that the object can perform. In object-oriented programming, classes are used to define the blueprint for creating objects, and the members of a class define the characteristics and capabilities of those objects. Therefore, a member of a class can be either an attribute or a method.
6.
A class is...
Correct Answer
C. An abstract definition of an object
Explanation
A class is an abstract definition of an object because it serves as a blueprint or template for creating specific instances of that object. It defines the properties and behaviors that the object will have, but it is not the actual object itself. The class provides a way to organize and structure the code, allowing for code reusability and modularity.
7.
Which package do you need to use widgets such as JApplet, JFrame, JPanel and JButton?
Correct Answer
A. Javax.swing
Explanation
The correct answer is javax.swing. This is because javax.swing is the package that provides classes for creating and managing graphical user interface (GUI) components in Java. It includes classes such as JApplet, JFrame, JPanel, and JButton, which are commonly used for building GUI applications. The other options, javax.gui, java.awt, and java.swing, are not valid packages in Java.
8.
Which one needs a web page to run
Correct Answer
C. A Java Applet
Explanation
A Java Applet needs a web page to run because it is a small application that is designed to be embedded within a web page. It is executed within a web browser's Java Virtual Machine (JVM) and requires the web page's HTML code to provide the necessary environment for its execution. Unlike Java Applications or Stand-Alone Applications, which can be run independently on a computer, a Java Applet relies on the web page infrastructure to function properly.
9.
How to define a JButton with the caption test?
Correct Answer
B. JButton aButton=new JButton("test");
Explanation
The correct answer is "JButton aButton=new JButton("test");". This is the correct way to define a JButton with the caption "test". In Java, when creating an object, the syntax is "new ClassName()", and in this case, the class name is JButton. The caption "test" is passed as a parameter within double quotes.
10.
The last value in an array called ar can be found at index:
Correct Answer
D. Ar.length - 1
Explanation
The last value in an array can be found at the index ar.length - 1 because arrays in most programming languages are zero-based, meaning the first element is at index 0. Therefore, the last element will be at the index one less than the length of the array.
11.
What would display from the following statements? int [ ] nums = {1,2,3,4,5,6}; System.out.println((nums[1] + nums[3]));
Correct Answer
A. 6
Explanation
The code initializes an array called "nums" with the values {1,2,3,4,5,6}. It then prints the sum of the values at index 1 and index 3 of the array, which are 2 and 4 respectively. Therefore, the output will be 6.
12.
What loop will display each of the numbers in this array on a separate line: float [ ] nums= {1.1f, 2.2f, 3.3f};
Correct Answer
A. For (int i =0; i < 3; i++) System.out.println( nums[i]);
Explanation
The correct answer is the first option, "for (int i =0; i < 3; i++) System.out.println( nums[i]);". This loop uses a for loop to iterate through the array "nums" and print each element on a separate line. The loop starts at index 0 and continues until the index is less than 3, incrementing the index by 1 each time. The "System.out.println(nums[i])" statement prints the element at the current index "i" in the array.
13.
What displays from the following statements? String word = "abcde"; for (int i = 0; i <4; i+=2) System.out.print(word[i]);
Correct Answer
B. Ac
Explanation
The given code initializes a string variable "word" with the value "abcde". Then, it enters a for loop that starts with i=0 and increments i by 2 in each iteration until i
14.
If we declare int [ ] ar = {1,2,3,4,5,6}; The size of array ar is:
Correct Answer
C. 6
Explanation
The size of the array "ar" is 6 because the given declaration "int[] ar = {1,2,3,4,5,6};" initializes an array named "ar" with 6 elements. Each element is assigned a value from 1 to 6. Therefore, the size of the array is equal to the number of elements in it, which is 6.
15.
The following prototype shows that a Cylinder subclass is derived from a superclass called Circle
Correct Answer
C. Class Cylinder extends Circle
Explanation
The correct answer is "class Cylinder extends Circle". This is because the keyword "extends" is used to indicate that the class Cylinder is derived from the superclass Circle. In object-oriented programming, inheritance allows a subclass to inherit the properties and methods of its superclass, and in this case, the subclass Cylinder is inheriting from the superclass Circle.
16.
With inheritance, a derived subclass object can directly access any
Correct Answer
C. Public superclass member (and protected subclass members if it's in the same package)
Explanation
Additionally, a subclass can access protected members of the superclass regardless of the package if it is derived from the superclass. However, private superclass members are not directly accessible to the subclass.
17.
What is the role of the constructor?
Correct Answer
A. Create an instance of a class (an object)
Explanation
Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object.
18.
A class cannot be declared:
Correct Answer
A. Static
Explanation
There is no access modifier called "static" for class declarations in Java. "Static" is used for static members and methods within a class. So, a class cannot be declared as "static," but it can have static members or methods.
19.
Following code will result in: int num = 6.7;
Correct Answer
A. Compilation error
Explanation
The given code will result in a compilation error. This is because the variable "num" is declared as an integer, but it is being assigned a floating-point value (6.7). In Java, you cannot assign a floating-point value to an integer variable without explicit type casting.
20.
Following code will result in: int a1 = 5; double a2 = (float)a1;
Correct Answer
C. No errors
Explanation
The given code will not result in any errors. It initializes an integer variable "a1" with the value 5 and then assigns the value of "a1" to a double variable "a2" after casting it to a float. Since both int and float can be implicitly converted to double without any loss of precision, there will be no compilation or runtime errors.
21.
Following code will result in: int num = 8/0;
Correct Answer
C. Runtime Exception
Explanation
The code will result in a runtime exception because it is trying to divide a number by zero, which is not allowed in mathematics. This will cause a runtime error and the program will terminate abruptly.
22.
Following code will result in: float num = 5/0;
Correct Answer
C. Runtime Exception
Explanation
The code will result in a runtime exception because dividing a number by zero is not a valid operation in mathematics. This will throw an exception at runtime and the program will terminate abruptly.
23.
A class can be transient
Correct Answer
B. False
Explanation
A class cannot be transient. The "transient" keyword in programming is used to indicate that a variable should not be serialized when an object is being converted to a byte stream. However, it cannot be applied to a class itself. The "transient" keyword can only be used with variables within a class to specify that they should not be included in the serialization process. Therefore, the statement that a class can be transient is incorrect.
24.
Following code will result in: class A { int x = 1; public static void main(String [] args) { System.out.println("x is " + x); }}
Correct Answer
A. Compilation error
Explanation
The code will result in a compilation error because the variable "x" is not declared as static, which means it can only be accessed through an instance of the class. However, in the main method, which is static, there is no instance of the class A created to access the variable "x". Therefore, a compilation error will occur when trying to access the non-static variable from a static context.
25.
Following code will result in: class A { public static void main(String [] args) {B b = new A(); }} class B extends A {}
Correct Answer
A. Compile error
Explanation
The given code will result in a compile error because the object 'b' is being declared as type 'B' but it is being assigned a new instance of class 'A'. Since 'A' is not a subclass of 'B', this assignment is not valid and will cause a compile error.
26.
Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}
Correct Answer
C. 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 B to a variable of type A. The code will compile successfully and run without any issues.
27.
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 and called by any subclass of that class. This access modifier allows subclasses to inherit and use the protected methods from the parent class. It provides a level of accessibility that is more restricted than public, but less restricted than private. Therefore, the statement that protected methods can be called in any subclass of the class is true.
28.
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 directly used by the subclasses that inherit from the abstract class. This allows the abstract class to provide common functionality to its subclasses while still allowing them to implement their own specific behavior.
29.
Java keywords are written in lowercase as well as uppercase.
Correct Answer
B. False
Explanation
Java keywords are written in lowercase only. In Java, keywords are predefined reserved words that have specific meanings and cannot be used as identifiers for variables, methods, or classes. These keywords are case-sensitive, meaning that they must be written in lowercase letters. Writing keywords in uppercase would result in a compilation error. Therefore, the statement that Java keywords are written in lowercase as well as uppercase is incorrect.
30.
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 belongs to a particular class or interface. It returns true if the object is an instance of the specified class or interface, or one of its subclasses or implementations. The instanceof keyword is used in Java programming language to perform this check. Therefore, the answer "An operator and keyword" accurately describes the instanceof operator.
31.
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, memory is allocated on the stack to store its value. The stack is a fast and efficient way to allocate and deallocate memory for these types of variables.
32.
Can you compare a boolean to an integer?
Correct Answer
B. No
Explanation
A boolean is a data type that can only have two possible values: true or false. An integer, on the other hand, is a numeric data type that represents whole numbers. Since they are different data types, they cannot be directly compared.
33.
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 is declared as abstract, it means that it cannot be instantiated directly and can only be used as a base class for other classes. In this case, if class A implements an interface, it is not required to implement all the methods of that interface. The responsibility of implementing those methods is passed on to the concrete subclasses of A. Therefore, the correct answer is "No, not when A is abstract."
34.
Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?
Correct Answer
D. False
Explanation
When comparing the objects "a" and "b" 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 different objects with different memory locations. Therefore, the condition "a==b" evaluates to false.
35.
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 synchronization. When a thread calls the wait() method, it releases the lock on the object and waits for another thread to notify it. Similarly, when a thread calls the notify() or notifyAll() method, it notifies the waiting threads that they can resume execution. To ensure thread safety and prevent race conditions, these methods should only be called from synchronized blocks or methods.
36.
Inner classes can be defined within methods.
Correct Answer
A. True
Explanation
Inner classes can be defined within methods. This statement is true. In Java, it is possible to define a class inside another class, and this inner class can also be defined within a method. This allows for more flexibility in organizing and encapsulating code. Inner classes have access to the variables and methods of the outer class, and can be useful in implementing complex logic or creating specialized data structures within a specific method scope.
37.
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 a method at a time. It provides a way to synchronize the execution of multiple threads, preventing them from interfering with each other. When a thread encounters a synchronized block or method, it grabs the lock on the associated object and continues execution. This ensures that other threads have to wait until the lock is released before they can access the synchronized code. Therefore, the statement that "synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.
38.
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 corresponding block of code is executed and the switch statement terminates. If there is no default statement, and none of the cases match, the switch statement simply does nothing. Therefore, the correct answer is false.
39.
How can you prevent a member variable from becoming serialized?
Correct Answer
C. By marking it transient
Explanation
A transient variable is a variable that may not be serialized.
40.
What is the keyword used in java to create an object?
Correct Answer
B. New
Explanation
In Java, the keyword "new" is used to create an object. It is followed by the name of the class and parentheses, which can optionally contain arguments to be passed to the constructor of the class. The "new" keyword dynamically allocates memory for the object and initializes its state.
41.
What is the correct syntax for java main method?
Correct Answer
D. None of the above
Explanation
public static void main(String args[]) is the right format. String instead of string is the correct one.
42.
A null reference may be used to access a static variable or method.
Correct Answer
B. False
Explanation
A null reference cannot be used to access a static variable or method because static members belong to the class itself and not to any specific instance of the class. They can be accessed using the class name directly, without needing an instance of the class. A null reference represents the absence of an object and cannot be used to access any members, including static members. Therefore, the statement is false.
43.
Native methods can be 'abstract'
Correct Answer
B. False
Explanation
Native methods cannot be abstract. Native methods are methods that are implemented in a language other than Java, such as C or C++. These methods are declared with the "native" keyword and their implementation is provided outside of the Java code. Abstract methods, on the other hand, are methods that are declared without an implementation and must be implemented by the subclasses. Since native methods already have a specific implementation in a different language, they cannot be declared as abstract. Therefore, the correct answer is False.
44.
Which of the following is not a Java keyword?
Correct Answer
C. Integer
Explanation
"main" is a keyword used to denote the entry point of a Java program.
"try" is a keyword used to start a block of code that might cause exceptions.
"String" is a keyword used to declare string variables and objects.
"integer" is not a keyword in Java. The correct keyword for integer data types in Java is "int."
45.
What is an Applet ?
Correct Answer
B. A Java program that is run through a web browser
Explanation
An applet is a Java program that is designed to run within a web browser. It is a small application that is embedded within a web page and can be executed by a Java Virtual Machine (JVM) installed on the client's machine. Applets are used to add interactive features to websites and provide dynamic content. They have access to certain resources on the client's machine, such as the file system, but are restricted from performing certain actions for security reasons.
46.
Java runs on _______.
Correct Answer
D. All of the Above
Explanation
Java is a platform-independent programming language, which means it can run on multiple operating systems. It is designed to be compatible with Windows, Unix/Linux, and Mac operating systems. Therefore, the correct answer is "All of the Above" as Java can run on all of these platforms.
47.
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 executed within a web browser. It is typically written in Java and runs on the client-side. On the other hand, an application is a standalone program that is designed to be executed on a computer's operating system. It is not limited to running within a web browser. Therefore, the difference between an applet and an application is that applets are specifically designed to be run over the web, while applications can be run on any operating system.
48.
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 allow us to assign values to data and retrieve or modify those values as needed. By storing data in variables, we can access and manipulate it throughout the program, making it an essential component of computer programming.
49.
What is the proper way to declare a variable ?
Correct Answer
D. 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 and ensures that the variable can be used correctly in the program.
50.
What is an assignment statement ?
Correct Answer
D. Assigning a value to a variable
Explanation
An assignment statement is a programming construct that assigns a value to a variable. It is used to store and update data in a program. In this context, assigning a value to a variable means giving a specific value to a variable so that it can be used later in the program. This is a fundamental concept in programming as it allows for the manipulation and storage of data.