Public class Process {
Public static void main(
String[] args)
{
Test t = new Test();
t.finalize();
t=null;
Runtime rt = Runtime.getRuntime();
rt.gc();
System.out.println(�Finalization Completed�);
}
}
class Test {
Protected void finalize() throws Throwable {
System.out.println(�Finalize method invoked�);
}
Ans : Compile-time error. finalize() method throws Throwable so try-catch block is required.
Question : Is it necessary to declare try and catch? Pls explain the program
/* ====================================================================
(3)
class Superclass {
int x = 0;
int methodA()
{
return x;
}
}
class Subclass extends Superclass {
int x = 1;
int methodA()
{
return x;
}
}
class Q30 {
public static void main(String[] args)
{
Subclass sub = new Subclass();
System.out.println("sub.methodA()= " + sub.methodA());
Superclass sup = new Superclass();
System.out.println("sup.methodA()=" + sup.methodA());
Superclass sup1 = new Subclass();
System.out.println("sup1.methodA()=" + sup1.methodA() + process(sup1));
}
static int process(Superclass obj)
{
return obj.x;
}
}
Ans : Prints sub.methodA=1
Prints sup.methodA=0
Prints sup1.methodA=1 and sup1.x = 0
Question : (I) Superclass sup1 = new Subclass(); What it actually represents whether superclass object or subclass object?
(ii) Is this sup1 is a ref. To superclass or to subclass ? explain
(iii) System.out.println("sup1.methodA()=" + sup1.methodA() + process(sup1)); -
In this statement sup1.methodA() is calling the subclass methodA function
And process(sup1)) is printing the value of x in the superclass. In which situation
It will represent the subclass or will represent the superclasss. Please explain
The above 3 questions in detail.
/* =============================================================== */
(4)
class InheritanceTest extends Process {
int x=18;
public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}
InheritanceTest() {
System.out.println(true ^ true);
}
InheritanceTest(char c) {
System.out.println(c);
}
char InheritanceTest(char c) {
c='V';
return (char)c;
}
}
class Process {
int x=9;
Process() {
System.out.println("Starting Process...");
}
char InheritanceTest(int i) {
i='S';
return (char)i;
}
}
Ans : Prints Starting Process �, false, �S� and 9
Question : Pls explain how is this answer comes?
/* ============================================================== */
(5)
public class Q46 {
public static void main(String[]args) {
int x;
int y;
for (x = 0, y = 5 ; x < y ; x= x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
Ans : This loop is infinite
Question : if x = x++ means x = x = x + 1 so it will produce compiler error is it?
How is it generate infinite loop?
/* ========================================================= */
(6)
public class Q47 {
public static void main(String[] args) {
int y=9;
int x ;
for (x = 0; x < 3; x = ++x)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
Ans : 0 9
1 9
2 9
3 9
Question : pls explain how the statement x=++x works here? . How is it functions like a usual
For loop ?
/* =========================================================== */
(7)
public class Q1 {
static void processorB() {
System.out.println(" is a boolean Operator.");
}
static Q1 processorA(){
Q1 q=null;
if (("java").startsWith("null"))
return q;
else
return null;
}
static {
System.out.print("Java".startsWith(""));
}
public static void main(String[] args) {
processorA().processorB();
}
}
Ans : True is a boolean operator
Question : Pls explain how is this ?
/* =============================================================== */
(8)
public class Q11 {
public static void main (String[] args) {
float f1 = Float.NaN;
double d1 = Double.NaN;
System.out.println(f1 == Float.NaN);
System.out.println(f1 <= Float.NaN);
System.out.println(f1 < Float.NaN);
System.out.println(f1 > Float.NaN);
System.out.println(f1 >= Float.NaN);
System.out.println(f1 != Float.NaN);
System.out.println(d1 == Double.NaN);
System.out.println(d1 <= Double.NaN);
System.out.println(d1 < Double.NaN);
System.out.println(d1 >= Double.NaN);
System.out.println(d1 > Double.NaN);
System.out.println(d1 != Double.NaN);
// System.out.println(Float.NaN);
}
}
Ans : System.out.println(f1 != Float.NaN);
System.out.println(d1 != Double.NaN);
Only the above two statements returns true.
Question : How pls explain?
/* ============================================================= */
(9)
class Process6
{
Process6()
{
write();
}
void write()
{
System.out.println(Thread.activeCount());
}
}
class Test6 extends Process6
{
public static void main(String[] args)
{
Process6 t = new Test6();
}
public void write()
{
System.out.println(Thread.interrupted());
}
}
Ans : Prints false
Question: ( ie) Here the write() method in the subclass is executed. How? Is the write() method
Of the superclass to be executed? Pls explain?
/* ============================================================= */
(10)
class Process
{
String s="Java";
Process()
{
write();
}
private void write()
{
System.out.println(Thread.activeCount());
}
}
class Test extends Process
{
String s="JavaScript";
public static void main(String[] args)
{
Process t = new Test();
t.write();
System.out.println(t.s);
}
public void write()
{
System.out.println(Thread.interrupted());
}
}
Ans : Compile-time error occurs
Question : Why compile time error occurs? Pls explain.
/* ============================================================== */
(11)
class Process8
{
boolean b;
public Process8(int x) {
System.out.println((char)x);
}
public void Process8(char c) {
if(b = java.lang.Character.isJavaIdentifierStart(c))
System.out.println(b);
}
public Process8(short s) {
System.out.println((char)s);
}
}
public class Test8
{
Process8 p3 = new Process8('V');
public static void main(String[ ] args)
{
Process8 p4 = new Process8('_');
}
static Process8 p1 = new Process8('J');
static Process8 p2 = new Process8('$');
}
Ans : Prints �J�
Prints �$�
Prints �_�
Question : The ans should be �J�, �$�, �V� and �_� is it so? Why �V� is not printed? Pls explain.