• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

javaprogramming

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this a joke ???
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavitha,
Q No: 04
---------------
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;
}
}
------------------------------
Answer:
The answer is printed because of inheritance.
Whenever a subclass constructor is called, the superclass constructor is called. so the msg in superclass constructor msg
"starting process.." is printed.
Then the current[subclass] constructor message false is printed
ie. true ^ true = false [ ^ == XOR ]
Then, the InheritanceTest(int i ) method in sub class is called, this is due to RunTime determination of object, so the reference is to the sub class, so the subclass mthd is invoked, instead of what you thought[ base constructor]
Then, the variable in base class is printed ["9"] .This is because during inheritance, methods are determined at runtime & member variables are determined at compile time.So, this happened.
Thanks & Regards
ANaveenS

------------------
ANaveenS
 
Naveen Arumugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavitha,
QNo#1
-----------------------
public class Process
{
public static void main(String[] args) throws Throwable //Exception
{
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");
}
}
------------------------------------
Answer:
The above program is a changed version, the method won't compile without the throws Throwable addition to the main method...
U might not even use throws Exception , because Throwable is the parent class fro Exception. Alternatively u could use try...catch(Throwable) also.
The reason u should use try/catch or throws clause is that the method u r using is supposed to throw an exception[of course its [parent] . Moreover its not a runtime exception, so u've to explicitly handle it!
Hope went into heart.
Rgds,
ANaveenS
 
Naveen Arumugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavitha,
I've presented a complete example with which U can understand easily.
Points to ponder:
* object methods are determined at runtime
* object variables are determined at compile time
* class methods are determined at compile time
* class variables are determined at compile time
-------------------------------------------------------codeExample&RunStartsHere--------------------------------------------------------------------------
/*
Everything in parent is in odd 9, 99
Everything in child is even 8,88
*/
class Parent
{
int memberVariable = 9;
static int classVariable = 999;
int memberMethod()
{
return memberVariable;
}
static int classMethod()
{
return classVariable;
}
}
class Child extends Parent
{
int memberVariable = 8;
static int classVariable = 88;
int memberMethod()
{
return memberVariable;
}
static int classMethod()
{
return classVariable;
}
}
class Check
{
public static void main(String args[])
{
Parent p = new Parent();
System.out.println("Using Parent p = new Parent();....");
System.out.println(p.memberMethod());
System.out.println(p.classMethod());
System.out.println(p.memberVariable);
System.out.println(p.classVariable);
System.out.println();
Child c = new Child();
System.out.println("Using Child c = new Child();....");
System.out.println(c.memberMethod());
System.out.println(c.classMethod());
System.out.println(c.memberVariable);
System.out.println(c.classVariable);
System.out.println();
p = c;
//c = p; //compile error check Y
System.out.println("Using p = c;....");
System.out.println(p.memberMethod());
System.out.println(p.classMethod());
System.out.println(p.memberVariable);
System.out.println(p.classVariable);
}
}
/* output:
Using Parent p = new Parent();....
9
999
9
999
Using Child c = new Child();....
8
88
8
88
Using p = c;....
8
999
9
999
*/
-------------------------------------------------------codeExample&RunEndsHere-----------------------------------------------------------------------------
Rgds,
ANaveenS
------------------
ANaveenS
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavitha,
In future, please only ask one question per post It's very difficult to read through three screens of questions at one time ... most of us don't have long attention spans
Thanks.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic