# Anu Kris

Greenhorn
since Oct 01, 2001
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
0
0
Total given
0
Given in last 30 days
0
Scavenger Hunt Ranch Hand Scavenger Hunt Greenhorn Scavenger Hunt

## Recent posts by Anu Kris

thanks for the help suresh
20 years ago
iam sorry.....ignore the previous msg.....value of i=7,i got it.Thanks for the help
20 years ago
the value of j at the end of the for loop is 11...i thought so
20 years ago
class B{
protected int x;
B(){
x=10;
}
B(int a){
x=a;
}
void ml(){x=20;}
void ml(int x){this.x=x;}
}
public class A{
int x;
A(){
x=20;}
A(int x){this.x=x;}
void ml(B x){
this.x=x.x++;
}
Public static void main(String args[]){
A x=new A(20);
B y=new B();
S.o.p(x.x);
x.ml(y);
S.o.p(x.x);
x.ml(30);
S.o.p(y.x);
((A)x).ml(y);
S.o.p(y.x);
}
}
ans is 20,10,30,31,pl explain
20 years ago
i still have a doubt of how value of j=7...when the loop completes the value of j is 11.Pl correct me is iam wrong
20 years ago
i still have a doubt of how value of j=7...when the loop completes the value of j is 11.Pl correct me is iam wrong
20 years ago
sorry the answer is 3.thanks nain
20 years ago
Why does the 'y' variable get reset to '0' when it completes the first cycle of the inner loop?
Also, doesn't the increment value at the end of a for statement take place before the code is processed inside the loop? It seems like the first time through the x & y values should be '1'?
==========
y gets reset to 0 every time it enters the loop again.
also for every initial value of the variable the body of the loop gets executed.hope this helps
20 years ago
class Q29 {
public static void main(String[] args) {
int j = 0;
Q29 test = new Q29();
try {
for (j=1 ; j < 5 ; j++)
{
j+= j;
}
int i = test.process() / (j = 5);
}
catch (Exception e) {
System.out.println(e);
System.out.println("Value of j = " + j);
}
}

int process() throws Exception {
throw new Exception("Exception Encountered");
}
}
What is the Output?
1.Prints Value of j = 2
2.Throws Arithmetic Exception.
3.Prints java.lang.Exception : Exception Encountered and Value of j = 7.
4.Prints Value of j = 10
5. Prints java.lang.Exception : Exception Encountered and Value of j = 2.
can anyone explain.Thanks
20 years ago
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;
}
}
What is the Output?
1.Prints Starting Process ? false, �S?and 18
2.Prints false, �V?and 9
3.Prints true, �V?and 9
4.Prints Starting Process ?, true, �V?and 9
5.Prints Starting Process ? false, �V?and 9
6.Prints Starting Process ? false, �V?and 18
7.Prints Starting Process ? false, s and 9
8.Prints Starting Process ? true, �R and 18
9.Prints Starting Process ? true, �V?and 18

A simple procedure to remember is :
1) All methods are invoked according to the object type of the variable, but for that the same signature method has to exist in the reference type otherwise its a compiler error.
2) All variables (static, nonstatic) are invoked according to the reference type of the variable.
Here the reference type of p is Process and object type is InheretanceTest.
The program invokes with a char argument, and you see there is no char argument method in Process. But still compiler promotes char to int and thereby maintains its rule for method invoction.
if you add the follwoing method in Process, then the method invoked will be from InheretanceTest
char InheritanceTest(char c) {
c='V';
return (char)c;
}

I know its confusing but if you give it some time you'll understand. Hope it helps.
Asma Zafar.
Sun Certfied Programmer for Java2 Platform.
=====i can understand both the rules of 1 and 2.but i think this stmt is misleading-
**The program invokes with a char argument, and you see there is no char argument method in Process.**
FOr meth invocation it checks the ob type in this case inheritencetest...but this method is overloaded and thus b'coz of implicit conversion chooses the meth in base calss with int arg. correct me if iam wrong
20 years ago
boolean in java take 2 values either true/false and by default it is false.It is not 0 and 1 and the size is 1 bit
20 years ago
1.for the first doubt,i think the coding is incomplete and the answer 1234 gets printed which is the value of i.
2.after the exception is caught ,finally will be executed,even if the exception was not caught finally will be executed and the normal flow of the program will continue.So the result is 134
20 years ago
This is how it works.
1.first time x=0 and y=0,so it checks condition x==y,it is true so it continues with the inner loop incrementing y.
2.now x=0,y=1,now the test case is false and it prints out the value of x and y i.e 0 and 1.
3.Again y is incremented,so x=0 and y=2,test case is false and prints the value of x=0 and y=2.
4.Now the inner loop is completed as test condition is y<3,so now it continues with the outer loop so now x=1 and y=0 and it prints this value.
5.now the inner y loop is incremented i.e for x=1 and y=1 ,test case is true so it continues with the inner loop.
6.now x=1,y=2 and it prints the value.so now both inner and outer loop are completed.
7.the valued printed are x=0,y=1
x=0,y=2
x=1,y=0
x=1,y=2
20 years ago