Instruction
- Total number of questions : 20
- Time allotted : 30:00 Minutes:Seconds.
- Each question carry 1 mark, no negative marks.
- DO NOT refresh the page.
- All the best
Java Programming [ Question No: 1 To 20 ] :
Which of the given options best describes the truthfulness of the following statements?
i) When a Java program is compiled, it generates Byte code.
ii) SOAP and RESTFUL are java web services.
iii) PATH Environment variable identifies the location of Java compiler.
iv) The || logical operator returns true if both statements are true.
What will be the output of the program?
import java.util.*;
public class Listing86
{
static class Item
{
String sku;
Item(String sku) { this.sku = sku; }
public boolean equals(Object o)
{
return ((Item) o).sku.equals(sku);
}
public int hashCode() { return sku.hashCode(); }
}
public static void main(String[] args)
{
Set<Item> set = new HashSet<Item>();
set.add(new Item("x"));
System.out.println(set.contains("x"));
}
} Which of these correctly schedules a task to run after a delay?
Which one of the following statements about what using a raw type such as a plain List costs is true?
Which one of the following statements about how a Comparator that is inconsistent with equals affects a sorted set is true?
What will be the output of the program?
public class Item24
{
void execute()
{
String bits = Integer.toBinaryString(10);
String hex = Integer.toHexString(255);
System.out.println(bits + " " + hex);
}
public static void main(String[] args)
{
Item24 demo = new Item24();
demo.execute();
}
} Consider using a plain object purely as a lock. Which statement is correct?
Iterating a CopyOnWriteArrayList - which of the following is true?
Consider what starvation means for a thread. Which statement is correct?
Consider a notify that arrives before the matching wait. Which statement is correct?
// thread A // thread B
synchronized (lock) { synchronized (lock) {
lock.notify(); lock.wait();
} } Which of these statements about invoking run() on a thread that is never started is correct?
Thread t = new Thread(() -> System.out.println(
Thread.currentThread().getName()));
t.run(); How a thread is asked to stop - which of the following is true?
Which line is an inappropriate use of an assertion?
public class Sample22
{
public static void main(String[] args)
{
String home = System.getProperty("app.home");
assert home != null : "app.home not set";
System.out.println(home.trim());
}
}
The program is run with java -ea Probe38 - what will be the output?
public class Probe38
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
assert i < 3 : i;
System.out.print(i);
}
}
} The program is run with java -ea -da Sample37 - what will be the output?
public class Sample37
{
public static void main(String[] args)
{
double rate = 0.0;
assert rate > 0.0 : "rate not set";
System.out.println("rate " + rate);
}
} The program is run with java -ea:com.acme.util... com.acme.util.Fragment30 - what will be the output?
package com.acme.util;
public class Fragment30
{
public static void main(String[] args)
{
assert false;
System.out.println("ran");
}
} The program is run with java -ea Listing17 - what will be the output?
public class Listing17
{
public static void main(String[] args)
{
try
{
assert false;
}
finally
{
System.out.println("finally");
}
}
}
class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
Which three statements are true?
1.Assertion checking is typically enabled when a program is deployed.
2.It is never appropriate to write code to handle failure of an assert statement.
3.Assertion checking is typically enabled during program development and testing.
4.Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
5.Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
Java Programming [ Question No: 1 To 20 ] :
Which of the given options best describes the truthfulness of the following statements?
i) When a Java program is compiled, it generates Byte code.
ii) SOAP and RESTFUL are java web services.
iii) PATH Environment variable identifies the location of Java compiler.
iv) The || logical operator returns true if both statements are true.
Your Answer : (Not Answered)
Correct Answer : A
Statements i, ii and iii are true and iv is false. Compiling a Java program produces byte code, SOAP and RESTful are accepted in the key as Java web services, and the PATH environment variable gives the location of the Java compiler so that it can be run from any directory. The || operator is the logical OR, which returns true when at least one of the operands is true, and it is && that needs both to be true. So the fourth statement is false.
What will be the output of the program?
import java.util.*;
public class Listing86
{
static class Item
{
String sku;
Item(String sku) { this.sku = sku; }
public boolean equals(Object o)
{
return ((Item) o).sku.equals(sku);
}
public int hashCode() { return sku.hashCode(); }
}
public static void main(String[] args)
{
Set<Item> set = new HashSet<Item>();
set.add(new Item("x"));
System.out.println(set.contains("x"));
}
} Your Answer : (Not Answered)
Correct Answer : B
contains passes the String to Item.equals, which casts its argument to Item without an instanceof guard and throws ClassCastException. A correct equals begins by testing the type and returns false for anything else, which is why the instanceof check is not optional.
Which of these correctly schedules a task to run after a delay?
Your Answer : (Not Answered)
Correct Answer : D
Correct answer: ScheduledExecutorService s = Executors.newScheduledThreadPool(1); s.schedule(task, 5, TimeUnit.SECONDS);
Scheduling requires a ScheduledExecutorService, which a fixed thread pool is not, and the delay must carry a TimeUnit so that the number is unambiguous. Executors is a factory and has no schedule method of its own.
Which one of the following statements about what using a raw type such as a plain List costs is true?
Your Answer : (Not Answered)
Correct Answer : A
A raw type switches off the guarantees generics exist to give, so anything can go in and the failure appears at the point of retrieval instead. It is not the same as List<Object>, which is checked; the raw form exists only for compatibility with pre-generics code.
Which one of the following statements about how a Comparator that is inconsistent with equals affects a sorted set is true?
Your Answer : (Not Answered)
Correct Answer : A
A sorted set's notion of duplication comes entirely from the comparison, so two elements the comparator calls equal are treated as one even when equals disagrees. The javadoc warns that such a set violates the general contract of Set, while still working; it just no longer means what a reader expects.
What will be the output of the program?
public class Item24
{
void execute()
{
String bits = Integer.toBinaryString(10);
String hex = Integer.toHexString(255);
System.out.println(bits + " " + hex);
}
public static void main(String[] args)
{
Item24 demo = new Item24();
demo.execute();
}
} Your Answer : (Not Answered)
Correct Answer : D
toBinaryString and toHexString return the digits alone, with no 0b or 0x prefix, and the hexadecimal letters are lower case. So 10 becomes 1010 and 255 becomes ff. Upper case would need toUpperCase() on the result.
Consider using a plain object purely as a lock. Which statement is correct?
Your Answer : (Not Answered)
Correct Answer : B
Every object carries a monitor, which is why private final Object lock = new Object(); is a complete lock. Making it private and final is the point: nothing outside can acquire it, and the reference cannot be reassigned to leave threads locking different objects.
Iterating a CopyOnWriteArrayList - which of the following is true?
Your Answer : (Not Answered)
Correct Answer : C
The iterator holds the array as it was when iteration began, so concurrent modifications are invisible to it and cannot invalidate it. The cost is on the write side, which copies the whole array, making the class suitable for read-mostly data such as listener lists.
Consider what starvation means for a thread. Which statement is correct?
Your Answer : (Not Answered)
Correct Answer : B
In starvation the system as a whole progresses but one thread keeps losing out, typically because greedy neighbours hold a shared lock most of the time. That is different from deadlock, where nothing progresses, and from livelock, where threads react to each other and stay busy achieving nothing.
Consider a notify that arrives before the matching wait. Which statement is correct?
// thread A // thread B
synchronized (lock) { synchronized (lock) {
lock.notify(); lock.wait();
} } Your Answer : (Not Answered)
Correct Answer : B
A monitor keeps no record of notifications. If notify() runs before the other thread reaches wait(), the signal is simply discarded and the later wait blocks with nothing left to wake it. This is the lost-wakeup problem, and it is why the condition is tested inside a synchronized region before waiting at all.
Which of these statements about invoking run() on a thread that is never started is correct?
Thread t = new Thread(() -> System.out.println(
Thread.currentThread().getName()));
t.run(); Your Answer : (Not Answered)
Correct Answer : D
Invoking run() directly is an ordinary method call, so nothing is scheduled and the body executes on whichever thread made the call, here main. The Thread object is created but never started, so it stays in the NEW state for the whole program. Replacing run() with start() is what makes it concurrent.
How a thread is asked to stop - which of the following is true?
Your Answer : (Not Answered)
Correct Answer : C
stop(), suspend(), resume() and destroy() are all deprecated. The javadoc recommends replacing them with code that sets a variable indicating the thread should stop, which the thread checks regularly before returning from run() in an orderly fashion. Ending a thread is always cooperative.
Which line is an inappropriate use of an assertion?
public class Sample22
{
public static void main(String[] args)
{
String home = System.getProperty("app.home");
assert home != null : "app.home not set";
System.out.println(home.trim());
}
}
Your Answer : (Not Answered)
Correct Answer : B
A system property is external configuration, not an internal invariant, so its absence is a real runtime condition the program must handle whether or not -ea was passed. Disabled, the check is skipped and the next line throws NullPointerException with no explanation of the cause.
The program is run with java -ea Probe38 - what will be the output?
public class Probe38
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
assert i < 3 : i;
System.out.print(i);
}
}
} Your Answer : (Not Answered)
Correct Answer : B
The assertion is checked at the top of each iteration. It passes for i equal to 0, 1 and 2, each of which is printed, and fails when i reaches 3. The error is thrown before that iteration's print, so the visible output is 012 followed by an AssertionError whose detail message is 3.
The program is run with java -ea -da Sample37 - what will be the output?
public class Sample37
{
public static void main(String[] args)
{
double rate = 0.0;
assert rate > 0.0 : "rate not set";
System.out.println("rate " + rate);
}
} Your Answer : (Not Answered)
Correct Answer : A
Neither switch carries an argument, so both apply to the same set of classes and the later one settles it: assertions end up disabled, the check is skipped and the program prints rate 0.0. Ordering only decides the outcome when two switches have the same specificity; a more specific switch beats a general one wherever it appears.
The program is run with java -ea:com.acme.util... com.acme.util.Fragment30 - what will be the output?
package com.acme.util;
public class Fragment30
{
public static void main(String[] args)
{
assert false;
System.out.println("ran");
}
} Your Answer : (Not Answered)
Correct Answer : B
The trailing three dots mean the named package and all of its subpackages. Fragment30 is in com.acme.util, so assertions are enabled for it, the false assertion is evaluated and an AssertionError is thrown before the print. Without the dots the switch would name a class rather than a package.
The program is run with java -ea Listing17 - what will be the output?
public class Listing17
{
public static void main(String[] args)
{
try
{
assert false;
}
finally
{
System.out.println("finally");
}
}
} Your Answer : (Not Answered)
Correct Answer : A
A finally block runs whether the try completes normally or is left by a throw, and AssertionError is no exception to that rule. So finally is printed first, and the error then continues to propagate out of main because nothing caught it. finally does not swallow the error.
class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?
Your Answer : (Not Answered)
Correct Answer : D
Option D is correct. Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6.
Option A is wrong. Because the reference to obj1 is stored in obj2[0]. The Object obj1 still exists on the heap and can be accessed by an active thread through the reference stored in obj2[0].
Option B is wrong. Because it is only one of the references to the object obj1, the other reference is maintained in obj2[0].
Option C is wrong. The garbage collector will not be called here because a reference to the object is being maintained and returned in obj2[0].
import java.io.*;
public class MyProgram
{
public static void main(String args[])
{
FileOutputStream out = null;
try
{
out = new FileOutputStream("test.txt");
out.write(122);
}
catch(IOException io)
{
System.out.println("IO Error.");
}
finally
{
out.close();
}
}
}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
Your Answer : (Not Answered)
Correct Answer : D
Any method (in this case, the main() method) that throws a checked exception (in this case, out.close() ) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to out.close() in the finally block must fall inside a (in this case nested) try-catch block.
Which three statements are true?
1.Assertion checking is typically enabled when a program is deployed.
2.It is never appropriate to write code to handle failure of an assert statement.
3.Assertion checking is typically enabled during program development and testing.
4.Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
5.Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.
Your Answer : (Not Answered)
Correct Answer : B
(1) is wrong. It's just not true.
(2) is correct. You're never supposed to handle an assertion failure.
(3) is correct. Assertions let you test your assumptions during development, but the assertion code-in effect-evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.
(4) is wrong. See the explanation for (5) below.
(5) is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages".
The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class.
Marks
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20