aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubts in 4 questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubts in 4 questions" Watch "doubts in 4 questions" New topic
Author

doubts in 4 questions

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Could anyone answer these q's.
Q. Cosider the code bellow,
import java.lang.*;
public class Test {
void f() {
System.out.println("In Exception");
throw new RuntimeException("From f()");
}
void g() {
System.out.println("Before");
f();
System.out.println("After");
}
public static void main(String[] args) {
Test s = new Test();
s.g();
}

class B{
int x;
B(){ x = 100;}
}
class A{
int x;
A(){x = 20;}
void m1(final B y){y.x = 10;}
void m(final B y){B x = y; x.x++; x = new B();}
public static void main(String arf[]){
A x = new A();
B b = new B();
System.out.println(x.x);
x.m1(b);
System.out.println(b.x);
x.m1(b);
System.out.println(b.x);
((A)x).m(b);
System.out.println(b.x);
}
}
a. will give Compiler error, becuase final can not be modified.
b. will give compiler error, because invalid final declaration
c. will give runtime error, because final gets modified.
d. Compile and runs fine.
Q What is the output of this program
public class ShortCircuit {
static boolean test1(int val) {
System.out.print(" test1 called ");
return val < 1;
}
static boolean test2(int val) {
System.out.print(" test2 called ");
return val < 2;
}
static boolean test3(int val) {
System.out.print(" test3 called ");
return val < 3;
}
public static void main(String[] args) {
if(test1(0) && test2(2) && test3(2))
System.out.println(" 1st expression is true");
else
System.out.println(" 1st expression is false");
if(test3(2) | | test2(2) | | test1(0))
System.out.println(" 2st expression is true");
else
System.out.println(" 2st expression is false");
}
}

Q. Consider the code
class A { static int i = 0;
public String toString() { return "Object A # " + i; }
A() { i++;}
}
public class VarArgs {
static void f(Object[] x) {
for(int i = 0; i < x.length; i++)
System.out.println(x[i]);
}
public static void main(String[] args) {
f(new Object[] {
new Integer(47),
new Float(3.14), new Double(11.11) });
f(new Object[] {"one", "two", "three" });
f(new Object[] {new A(), new A(), new A()});
}
}
a. This code will not compile
b. Compiles fine and throws run time exceptions.
c. Compiles & runs fine.
d. Compiles & runs but give no output
Q
public class Arrays1 {
public static void main(String[] args) {
int[] a1 = { 1, 2, 3, 4, 5 };
int a2[];
float f1[] = new float[5];
a2 = a1;
for(int i = 0; i < a2.length; i++)
f1[i] = (float)a2[i]++;
for(int i = 0; i < f1.length; i++)
System.out.println(
"a1[" + i + "] = " + f1[i]);
}
}
a. Program will give compiler error, because incompatable array type
b. output will be 0.0 0.0 0.0 0.0 0.0
c. output will be null null null null null
d. program will give complier error, because possible use of array before its
initialization.
e. will give 1.0 2.0 3.0 4.0 5.0
true or false
.
1. Integer value can be safely converted to a char with explicit casting.
2. char x = 092; will give compiler error.
i have one more doubt
i know that final methods may not be overidden but if that final method is declared private then can that method be used in subclass it will not be visible in the subclass.
Can private methods be overidden to be less private.

nss
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

i know that final methods may not be overidden but if that final method is declared private then can that method be used in subclass it will not be visible in the subclass.
Can private methods be overidden to be less private.

Whether final or not, private methods are *not* inherited in the subclass, so they can't be used/invoked as it is. Since they're not visible in the subclass, you can always declare a method with same signature in the subclass, but this is not overriding.
Private methods can not be overriden, other (protected default/package) methods can be overriden to be more public, i.e. less restrictive (private, so to speak).
Other questions are pretty interesting too, will watch this post to know views from our fellow ranchers and bartenders.
Regards,
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please look at the first question, it seems that the code is not that clear, you have the class Test and classes A and B which are not related to Test. Furthermore Test and A have a main method...
second question:
the output is
test1 called test2 called 1st expression is false
test3 called 2st expression is true
1st expression is false because the invocation of test2(2) returns false and thus the whole expression yields false without having to execute test3(2)
2nd expression is true because the invocation of test3(2) yields true and thus the whole expression yields true without having to invoke test2(2) and test1(0)
third question:
answer c: compiles and runs fine
output:
47
3.14
11.11
one
two
three
Object A # 3
Object A # 3
Object A # 3
fourth question:
answer e: will give 1.0 2.0 3.0 4.0 5.0
in fact the correct output is
a1[0] = 1.0
a1[1] = 2.0
a1[2] = 3.0
a1[3] = 4.0
a1[4] = 5.0
These are just a matter of running the code and looking at the output
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited October 18, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

OK!!
Found in JLS 8.4.3.3 final Methods -
All private methods are implicitly final. It is impossible to override them. Adding keyword "final" is redundant here.
And yes, thanks Val.
- Manish
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi Manish,
Can there be a method private final void a(){} in parent calss and private void a(){} in sub class.
what i feel is private methods are not seen in subclass and the method declared in subclass is completely a new method with no relation with a() in super class.
So it is possible.
Am i right?
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi
the problem with 3rd question is that array is containing elements of different type which confused me.
And in 4th question one type of array is converted into another,is that allowed.
Could u clear my doubts.
thanx in advance
neha
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Hi,
.
true or false
1. Integer value can be safely converted to a char with explicit casting.
2. char x = 092; will give compiler error.
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
1.true
2.true (yes compiler complains about a malformed integer number)
Originally posted by Neha Sawant:
Hi,
.
true or false
1. Integer value can be safely converted to a char with explicit casting.
2. char x = 092; will give compiler error.

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Neha Sawant:
Hi Manish,
Can there be a method private final void a(){} in parent calss and private void a(){} in sub class.
what i feel is private methods are not seen in subclass and the method declared in subclass is completely a new method with no relation with a() in super class.
So it is possible.
Am i right?

Abosolutely
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Actually,
The JLS sentence "private methods are implicitly final" has got me little confused, but I'll post a question abt. it in a separate post.
- Manish
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Please Neha place the code between "" and it wil be much easier to read. Thanks


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Well, read here to know what I mean:
http://www.javaranch.com/ubb/ubbcode.html
dylan shen
Greenhorn

Joined: Oct 20, 2001
Posts: 10

Originally posted by Neha Sawant:
Hi,
.
true or false
1. Integer value can be safely converted to a char with explicit casting.
2. char x = 092; will give compiler error.

--------------------------------------------------------------------------------
Hi, I don't think the 1st one is true, Interger is an object while char is a primitive type, Integer can't be converted to char. Am I right?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubts in 4 questions