• 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

Declarations and access control

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
************************************************
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
};
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic