ISRO | ISRO CS 2013 |问题63

以下Java程序的输出是什么?

null
Class Test
{
 public static void main (String [] args)
 {
 int x = 0;
 int y = 0;
 for (int z = 0; z < 5; z++)
 {
 if((++x > 2) || (++y > 2))
 {
 x++;
 }
 }
 System.out.println( x + " " + y);
 }
}

(A) 8 2 (B) 8 5 (C) 8 3 (D) 5 3 答复: (A) 说明: 通过应用短路技术,

z = 0: x = 1, y = 1, if condition false
z = 1: x = 2, y = 2, if condition false
z = 2: x = 3, if condition true and due
to short circuiting ++y is not evaluated,
x++ // x = 4
z = 3: x = 5, again if condition is true,
x++ // x = 6
z = 4: x = 7, condition true, x++ // x = 8

因此,选项(A)是正确的。 这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享