1.
Import myLibrary.*;
public class ShowSomeClass
{
// code for the class...
}
What is the name of the java file containing this program?
Correct Answer
B. ShowSomeClass.java
Explanation
The name of the java file containing this program is "ShowSomeClass.java" because in Java, the name of the file must match the name of the public class defined within it. In this case, the public class is "ShowSomeClass", so the file should be named "ShowSomeClass.java".
2.
Which of the following is TRUE?
Correct Answer
D. A class has always a constructor (possibly automatically supplied by the java compiler)
Explanation
A class in Java always has a constructor, which may be automatically provided by the Java compiler.
3.
String river = new String(“Columbia”);
System.out.println(river.length());
Correct Answer
C. 8
Explanation
The code creates a new String object named "river" with the value "Columbia". The length() method is then called on the "river" object, which returns the number of characters in the string. In this case, the string "Columbia" has 8 characters, so the output of the code will be 8.
4.
Which of the following may be part of a class definition?
Correct Answer
D. All of the above
Explanation
All of the options listed, which include instance variables, instance methods, and constructors, can be part of a class definition. Instance variables represent the attributes or properties of an object, instance methods define the behavior or actions that the object can perform, and constructors are special methods used to initialize objects of the class. Therefore, all of these components are commonly included in a class definition.
5.
What is different between a Java applet and a Java application?
Correct Answer
D. (A), (B) and (C).
Explanation
A Java applet and a Java application differ in several ways. Firstly, an application can generally be trusted, whereas an applet cannot. This means that an application has more privileges and can perform actions that an applet is restricted from doing. Secondly, an applet must be executed in a browser environment. It is designed to run within a web page and is subject to certain security restrictions imposed by the browser. Lastly, an applet is not able to access the files of the computer it runs on. This limitation ensures that applets cannot access or modify sensitive files on the user's machine. Therefore, the correct answer is (A), (B), and (C).
6.
Public class MyClass{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?
Correct Answer
A. MyClass mc = new MyClass();
Explanation
To instantiate an object of the MyClass class, the correct syntax is "MyClass mc = new MyClass();". This creates a new instance of the MyClass class and assigns it to the variable "mc". The "new" keyword is used to allocate memory for the object and invoke the constructor of the class to initialize its state.
7.
What is byte code in the context of Java?
Correct Answer
A. The type of code generated by a Java compiler
Explanation
Byte code refers to the type of code generated by a Java compiler. Java source code is compiled into byte code, which is a low-level representation of the code that can be executed by a Java Virtual Machine (JVM). This byte code is platform-independent and can be run on any device or operating system that has a JVM installed. The JVM then interprets and executes the byte code, making Java a portable language.
8.
What is garbage collection in the context of Java?
Correct Answer
C. When all references to an object are gone, the memory used by the object is automatically reclaimed.
Explanation
Garbage collection in the context of Java refers to the automatic process of reclaiming memory used by objects that are no longer referenced in a program. When all references to an object are gone, the memory occupied by that object is automatically freed up by the Java Virtual Machine (JVM). This helps in managing memory efficiently and prevents memory leaks.
9.
You read the following statement in a Java program that compiles and
executes.
submarine.dive(depth);
What can you say for sure?
Correct Answer
B. Dive must be a method
Explanation
Based on the given statement in the Java program, "submarine.dive(depth);", we can say for sure that "dive" must be a method. This is because the statement follows the syntax for calling a method in Java, where "submarine" is the object or instance on which the method "dive" is being called, and "depth" is a parameter being passed to the method. Therefore, "dive" must be a method that is defined within the class to which the "submarine" object belongs.
10.
Which will legally declare, construct, and initialize an array?
Correct Answer
D. Int myList [] = {4, 3, 7};
Explanation
The correct answer is "int myList [] = {4, 3, 7}". This statement declares, constructs, and initializes an array named "myList" with the values 4, 3, and 7. The array is of type int and the square brackets indicate that it is an array. The values are enclosed in curly braces and separated by commas, which is the syntax for initializing an array in Java.
11.
Which is a reserved word in the Java programming language?
Correct Answer
B. Native
Explanation
In the Java programming language, "native" is a reserved word. Reserved words are predefined keywords that have special meanings and cannot be used as identifiers (such as variable names or method names) in the program. The "native" keyword is used to indicate that a method is implemented in a language other than Java, typically in a native language like C or C++. This allows for the integration of platform-specific code into Java programs.
12.
Which is a valid keyword in java?
Correct Answer
A. Interface
Explanation
The correct answer is "interface". In Java, "interface" is a valid keyword that is used to define a collection of abstract methods. It is used to achieve abstraction and multiple inheritance in Java. Interfaces can be implemented by classes to provide specific implementations for the abstract methods defined in the interface.
13.
public interface Foo
{
int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
Correct Answer(s)
A. Final int k = 4;
B. Public int k = 4;
C. Static int k = 4;
Explanation
The correct answer is final int k = 4; because when a variable is declared as final, its value cannot be changed once it is assigned. In this case, the variable "k" is declared as final and assigned the value of 4, making it a constant. The other options, public int k = 4; and static int k = 4; are not equivalent because they do not restrict the variable from being changed.
14.
Which one is a valid declaration of a boolean?
Correct Answer
C. Boolean b3 = false;
Explanation
The correct answer is "boolean b3 = false;" because in Java, the boolean data type can only have two possible values: true or false. The other options are incorrect because "0" is not a valid boolean value, "'false'" is a string and not a boolean value, and there is no method called "Boolean.false()" to declare a boolean.
15.
What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Correct Answer
A. Finally
Explanation
The program will output "Finally". In this program, the code inside the finally block will always execute, regardless of whether there is a return statement in the try block. So, even though the return statement is encountered, the finally block will still execute and print "Finally" before the program terminates.
16.
What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
Correct Answer
C. Compilation fails.
Explanation
The program will fail to compile because the catch block for the ArithmeticException is placed after the catch block for the more general Exception. In Java, catch blocks must be ordered from most specific to most general, so the catch block for ArithmeticException should come before the catch block for Exception.
17.
What will be the output of the program?
public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
Correct Answer
D. Hello world Finally executing
Explanation
The program will output "Hello world Finally executing". This is because the try block is executed first, which prints "Hello world". Then, the finally block is executed, which prints "Finally executing". The finally block is always executed, regardless of whether an exception is thrown or not.
18.
What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
Correct Answer
C. Compilation fails
Explanation
The program will fail to compile because there is a type mismatch in line 7. The variable "x" is of type "int" while the variable "y" is of type "double". In Java, a boolean expression cannot be assigned the value of a double. Therefore, the line "boolean b = (x = y);" will cause a compilation error.
19.
Which class does not override the equals() and hashcode() methods, inheriting them directly from class Object?
Correct Answer
C. Java.lang.StringBuffer
20.
You need to store elements in a collection that guarantees that no duplicates are stored. Which one of the following interfaces provide that capability?
Correct Answer
A. Java.util.Map
Explanation
The correct answer is Java.util.Map. The Map interface in Java provides a collection that guarantees that no duplicates are stored. It is an associative data structure that stores key-value pairs, where each key is unique. If an attempt is made to add a duplicate key, the existing value associated with that key will be replaced. Therefore, using a Map ensures that no duplicate keys are stored.
21.
What is the name of the method used to start a thread execution?
Correct Answer
B. Start();
Explanation
The start() method is used to start the execution of a thread. When the start() method is called, it creates a new thread and invokes the run() method of the thread. The run() method contains the code that will be executed in the new thread. Therefore, start() is the correct method to start a thread execution. The init() method is used to initialize a thread, resume() method is used to resume a suspended thread, and run() method is the entry point for the thread's code.
22.
Which two are valid constructors for Thread?
Correct Answer(s)
A. Thread(Runnable r, String name)
B. Thread()
Explanation
The question asks for the valid constructors for the Thread class. The first constructor, Thread(Runnable r, String name), takes a Runnable object and a String as parameters. This constructor is used to create a new thread with a specific name. The second constructor, Thread(), is a default constructor that creates a new thread with no parameters. Both of these constructors are valid ways to create a new Thread object.
23.
Which cannot directly cause a thread to stop executing?
Correct Answer
C. Calling notify() method on an object.
Explanation
Calling the notify() method on an object cannot directly cause a thread to stop executing. The notify() method is used to wake up a single thread that is waiting on the object's monitor. It does not stop the execution of the thread itself. The other options mentioned, such as calling the SetPriority() method on a Thread object, calling the wait() method on an object, or calling the read() method on an InputStream object, can potentially affect the execution of a thread.
24.
Which of the following will directly stop the execution of a Thread?
Correct Answer
A. Wait()
Explanation
The wait() method is used to make a thread wait until another thread notifies it to resume. When a thread calls the wait() method, it releases the lock it holds, allowing other threads to execute. This means that the execution of the thread calling wait() is paused until it receives a notification. Therefore, wait() directly stops the execution of a thread until it is notified to continue.
25.
Which two of the following methods are defined in class Thread?
Correct Answer(s)
A. Start()
C. Run()
Explanation
The methods start() and run() are defined in the class Thread. The start() method is used to start a new thread and call the run() method. The run() method contains the code that will be executed when the thread is started. The notify() method is not defined in the class Thread and the terminate() method is not a valid method in the class Thread.