File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Declarations and access control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declarations and access control" Watch "Declarations and access control" New topic
Author

Declarations and access control

muhammedo
Greenhorn

Joined: Jul 14, 2001
Posts: 4
Can anybody help analyze these questions?
I have been trying to figure it out,....by the way this is one
of the questions from jqplus

question ID :957921513300
What will be the output of the following program ?
class Test
{
static int i1, i2, i3;
public static void main(String[] args)
{
try
{
test(i1 = 1, oops(i2=2), i3 = 3);
} catch (Exception e)
{
System.out.println(i1+" "+i2+" "+i3);
}
}
static int oops(int i) throws Exception
{
throw new Exception("oops");
}
static int test(int a, int b, int c) { return a + b + c; }
}
******************************************************
ANS 1,2, 0
******************************************************

Question ID :954959233442
What is the effect of compiling and running this class ?
public class TestClass
{
public static void main (String args [])
{
int sum = 0;
for (int i = 0, j = 10; sum > 20; ++i, --j) // 1
{
sum = sum+ i + j;
}
System.out.println("Sum = " + sum);
}
}
************************************************
why will print sum = 0
************************************************
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
As to your first question I don't know the answer.
Question ID :954959233442
In the for loop statement the boolean conditions is
sum > 20.
Sum is 0 so the for loop is never executed.
Hower if the forloop statement would have been
for (int i = 0, j = 10; sum < 20; ++i, --j) // sum < 20
then the output is: sum = 20.
Hope this helps

------------------
Preparing for the Java 2 Certification exam


Preparing for the Java 2 Certification exam
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
For ur TestClass ie 2nd code---Come on ! sum is never greater than 20 . How is it gonna go in the for loop ???
When u run Test i.e. u'r 1st code a call to the test() method results in the oops() method throwing an Exception which is caught by the corresponding catch block which in turn prints the value of the 3 variables .
And if u r wondering 'bout the variable values here's how u could explain it-----
Since i1,i2 & i3 are member variables & since they are not assigned any values upon declaration they are automatically assigned the value zero.
And before the Exception is thrown by the oops() method i1 is assigned 1 & i2 is assigned 2 but the assignment of i3 is never reached hence i3 stays zero .
And if u find this thing 'bout member variables strange try these 2 codes & see 4 urself
class TestDemo{
static int a,b,c;/*these are member variables & they do not need initialization . this code will compile & run*/
public static void main(String []ka){
System.out.println(a);
}
};
----------------------NEXT CODE-----------------
class TestDemo01{
public static void main(String []ka){
int a,b,c;/*these are automatic variables & they need initialization & this code won't compile*/
System.out.println(a);
}
};
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by muhammedo:

question ID :957921513300
What will be the output of the following program ?
class Test
{
static int i1, i2, i3;
public static void main(String[] args)
{
try
{
test(i1 = 1, oops(i2=2), i3 = 3);
} catch (Exception e)
{
System.out.println(i1+" "+i2+" "+i3);
}
}
static int oops(int i) throws Exception
{
throw new Exception("oops");
}
static int test(int a, int b, int c) { return a + b + c; }
}
******************************************************
ANS 1,2, 0
******************************************************

The first line in your try block is going to call the test method. Before it can do that it needs to evaluate the value of each of the operands in the arguement list. It does this from left to right and each one is fully evaluated before the next. So in this case the first thing it does is assign the value of 1 to i1, then it calls the oops method, however before it can do that it has to evaluate all of its arguements first. so it assigns the value of 2 to i2 then calls oops,a nd passes it the value of i2. In this code oops just throws an exception so no further evaluation of operands is done in the call to test. In other words it stopped right after it assigned the value of 2 to i2. Now it moves to the catch block for Exception and prints out the values of the three variables. It prints 1, 2, 0 because we assigned 1 to i1, then assigned 2 to i2, but it ended abruptly (with the Exception) before it could assign a value to i3 so i3 gets it default value that it was assigned when the class was first loaded.
The test method never got called as it ended abruptly while evaluating its arguements (calling test wouldn't have changed anything anyway).
Hope this helps
Dave

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declarations and access control