• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubts in 4 questions

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Neha place the code between "" and it wil be much easier to read. Thanks
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, read here to know what I mean:
http://www.javaranch.com/ubb/ubbcode.html
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic