Code: Select all
getArray()[index=2]++
Regards.
Moderator: admin
Code: Select all
getArray()[index=2]++
Code: Select all
public static int[] getArray()
{
return new int[]{0, 1, 4, 6, 8, 10};
}
public static void main(String[] args)
{
int index = 1;
int g = 0;
try
{
g = getArray()[index=2]++;
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("index = " + index);
System.out.println("index = " + g);
}}
Code: Select all
index = 2
index = 4
Code: Select all
g = getArray()[index=2]<<2;
No, because it is postfix.bbakla wrote:My intention is actually to print g.
if the ++ doesnt increment the variable index, shouldn't g be 5?
No, it doesn't. Not sure why you think so.I guess ++ operator increments g in this case
Is this the case ?In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Code: Select all
class X {
public static int [] apply(){
if (Y.i==Y.i)throw new NullPointerException();
return new int[]{10,8,4,6};
}
}
public class Y {
public static int i = 0;
public static void main(String[] args) {
try {
System.out.println(X.apply()[i = 3]);
} finally {
System.out.println(i);
}
}
}
Code: Select all
class Test {
public static int[ ] getArray() { return null; }
public static void main(String[] args) {
int index = 1;
try{
getArray()[index=2]++; //1
} catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Code: Select all
int temp= getArray()[index=2];
temp++;
Yes, i will remain 0. i = 3 will not be executed.qmwuzapz wrote:from the exam explanation:Is this the case ?In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Code: Select all
class X { public static int [] apply(){ if (Y.i==Y.i)throw new NullPointerException(); return new int[]{10,8,4,6}; } } public class Y { public static int i = 0; public static void main(String[] args) { try { System.out.println(X.apply()[i = 3]); } finally { System.out.println(i); } } }
Users browsing this forum: No registered users and 105 guests