File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Expressions! 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 "Expressions!" Watch "Expressions!" New topic
Author

Expressions!

Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
What do you think gets printed out(Don't compile and run :-))
1)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,++i + ++i);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
2)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,i+++i++);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Anil Kollur
Ranch Hand

Joined: Mar 25, 2000
Posts: 101

Hi! Surya,
I didn't compiled code but IMHO
1) 3,4,11
2)3,4,8
will be printed, correct me if I am wrong.
Anil.


Everyday is a Learning phase! Hence ask "What have I learnt today?"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Surya B:
What do you think gets printed out(Don't compile and run :-))
1)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,++i + ++i);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
2)
class Test
{
public static void main(String[] args)
{
int i=3;
print3(i,++i,i+++i++);
}
static void print3(int a, int b, int c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}

Answer of (1) is 3,4,11 and for (2) is 3,4,9
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Answer of (1) is 3,4,11 and for (2) is 3,4,9
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi All
Let me be frank,i was thoroughly confused with the question myself,i was goint through JLS and it had one such example,i just tweaked it a little bit to include here,the answers as you already have given is 3,4,11 for the first and 3,4,9 for the second..by the way one more funny question(atleast according to me)..what do u think is the answer to this silly and easy question???
class Test
{
public static void main(String[] args)
{
long l=3;
long [] a=new long[l];
System.out.println(a.length);
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
is it 1 ?
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi
Why do you think its one,i think you have considered variable 'l(el)' as 1(one)..it is alphabetl(el).What do you think is the answer now???
Surya
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
This should give a compiler error because the array size specifier should be an int.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
1 is the length of the array. The question tries to confuse with the length of a long primitive.
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
Hi vasansrini
Uvnik Gupta answer is correct,from JLS
The type of each dimension expression DimExpr must be an integral type, or a compile-time error occurs. Each expression undergoes unary numeric promotion.The promoted type must be int, or a compile-time error occurs; this means, specifically, that the type of a dimension expression must not be long.
So you can say
class Test
{
public static void main(String[] args)
{
int l=3; or char l=3 or short l=3 or byte l=3-
--IS OK
int [] a=new int[l];
System.out.println(a.length);
}
}
But you cannot have float,long or double.
Surya
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think Uvnik Gupta is right. ( i jumped to a conclusion too quick)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
what does it print and why ?
public class Test {
public static void main(String[] args) {
int i=0;
i=i++;
System.out.println(i);
}
}
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
I think it will print 0.As i is post incremented.
Please someone explain me why the answer to the 2nd. code is 9 not 8.
Thanks
mita
[This message has been edited by mita (edited July 27, 2000).]
hemant gome
Greenhorn

Joined: Jul 24, 2000
Posts: 15
Hi Mita,
int i=3;
print3(i,++i,i+++i++);
Java evaluates expressions Left -> Right.
i = 3;
++i = 4;
a = i++ = 4;
i=5;
b = i++ = 5;
i=6;
so a + b = 9;
got it ??
Hemant.


hem@nt
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
Hi Surya and Uvnik
Thanx for clearing the concept of DimExp.
Surya thanx for the questions and your explanation on that was quiet good.
Plese keep the good work going. Thanks.
[This message has been edited by Harry Chawla (edited July 29, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Expressions!
 
Similar Threads
about specific method and compile time type error
Binary numeric promotion
Objects (please can someone clear this up)
what identifier??
Array of Arrays ???