A Learning Portal from Recruitment India
Which constructs an anonymous inner class instance?
Runnable r = new Runnable() { };
Runnable r = new Runnable(public void run() { });
Runnable r = new Runnable { public void run(){}};
System.out.println(new Runnable() {public void run() { }});
Answer with explanation
Answer: Option DExplanation
It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.
A is incorrect because it doesn’t override the run() method, so it violates the rules of interface implementation.
B and C use incorrect syntax.
Workspace
Which of the following is not a Java features?
Dynamic
Architecture Neutral
Use of pointers
Object-oriented
Answer with explanation
Answer: Option CExplanation
The Java language does not support pointers; some of the major reasons are listed below:
One of the major factors of not using pointers in Java is security concerns. Due to pointers, most of the users consider C-language very confusing and complex. This is the reason why Green Team (Java Team members) has not introduced pointers in Java.
Java provides an effective layer of abstraction to the developers by not using pointers in Java.
Java is dynamic, architecture-neutral, and object-oriented programming language.
Workspace
Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
1 < x < 100 || x < 0
((x < 100) && (x > 1)) || (x < 0)
((x < 100) && (x > 1)) && (x < 0)
(1 > x > 100) || (x < 0)
Answer with explanation
Answer: Option BExplanation
((x < 100) && (x > 1)) || (x < 0)
Workspace
What is the output of the following code:
public class Test{
public static void main(String[] args){
int[] x = {120, 200, 016 };
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
}
}
120 200 14
120 200 016
016 is a compile error. It should be written as 16.
Answer with explanation
Answer: Option AExplanation
016 is an octal number. The prefix 0 indicates that a number is in octal.
Workspace
Suppose a class has public visibility. In this class we define a protected method. Which of the following statements is correct?
This method is only accessible from inside the class itself and from inside all subclasses.
In a class, you cannot declare methods with a lower visibility than the visibility of the class in which it is defined.
From within protected methods you do not have access to public methods.
This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
Answer with explanation
Answer: Option DExplanation
This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
Workspace
Which of the following statement is correct?
replace() method replaces all occurrences of one character in invoking string with another character.
replace() method replaces only first occurances of a character in invoking string with another character.
replace() method replaces all the characters in invoking string with another character.
replace() replace() method replaces last occurrence of a character in invoking string with another character.
Answer with explanation
Answer: Option AExplanation
replace() method replaces all occurrences of one character in invoking string with another character.
Workspace
Which of these is an incorrect array declaration?
int arr[] = new int[5]
int [] arr = new int[5]
int arr[]arr = new int[5]
int arr[] = int [5] new
Answer with explanation
Answer: Option DExplanation
Operator new must be succeeded by array type and array size.
Workspace
Modulus operator, %, can be applied to which of these?
Integers
Floating – point numbers
Both Integers and floating – point numbers.
None of the mentioned
Answer with explanation
Answer: Option CExplanation
Modulus operator can be applied to both integers and floating point numbers.
Workspace
Which of these process occur automatically by java run time system?
Serialization
Garbage collection
File Filtering
All of the mentioned
Answer with explanation
Answer: Option AExplanation
Serialization and deserialization occur automatically by java run time system, Garbage collection also occur automatically but is done by CPU or the operating system not by the java run time system.
Workspace
Which of these method can be used to increase the capacity of ArrayList object manually?
Capacity()
increaseCapacity()
increasecapacity()
ensureCapacity()
Answer with explanation
Answer: Option DExplanation
When we add an element, the capacity of ArrayList object increases automatically, but we can increase it manually to specified length x by using function ensureCapacity(x)
Workspace
Which of these functions is called to display the output of an applet?
display()
print()
displayApplet()
PrintApplet()
Answer with explanation
Answer: Option BExplanation
Whenever the applet requires to redraw its output, it is done by using method paint().
Workspace
Which of these method can be used to make the main thread to be executed last among all the threads?
stop()
sleep()
join()
call()
Answer with explanation
Answer: Option BExplanation
By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.
Workspace
Extending the thread class
Both of the above
None of these
Answer with explanation
Answer: Option CExplanation
A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called. The Main thread in Java is the one that begins executing when the program starts.
Workspace
Which of the followings are true about constructors?
1. A class can have more than one constructor.
2. They can be inherited.
3. Their address can be referred.
4. Constructors cannot be declared in protected section of the class.
5. Constructors cannot return values.
Only 1,2,4
1,2,4,5
1,4,5
1,3,5
Answer with explanation
Answer: Option CExplanation
1,4,5
The purpose of a constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronized while methods can be. Constructors do not have return types while methods do.
Workspace
The following example shows which XML Syntax rule,
< note date=” 12/11/2007″>
< to>Tove< /to>
< from>Jani< /from>
< /note>
XML Attribute Values Must be Quoted
All XML Elements Must Have a Closing Tag
XML Tags are Case Sensitive
XML Elements Must be Properly Nested
Answer with explanation
Answer: Option AExplanation
XML Attribute Values Must be Quoted
Workspace
import java.util.*;
public class Main
{
public static void main(String[] args) {
List<Integer> mylist = new ArrayList();
mylist.remove(0);
System.out.println(mylist.size());
}
}
Nothing is printed
Compilation Error
IndexOutOfBoundsException
Answer with explanation
Answer: Option DExplanation
IndexOutOfBoundsException
Workspace