Java Test 1

From Progzoo
Jump to navigation Jump to search
State the output from this program fragment:

<syntaxhighlight lang="java">

   int a = 5;
   int b = 7;
   a = a + b;
   b = a + b;
   System.out.println(b);

</syntaxhighlight>

State the output from this program fragment:

<syntaxhighlight lang="java">

   int a = 5;
   int b = 7;
   int c = 8;
   a = b;
   b = c;
   c = a;
   System.out.println(100*a+10*b+c)

</syntaxhighlight>

State the output from this program fragment:

<syntaxhighlight lang="java">

   String s = "Fourty two";
   String t = s.substring(7,8)+s.substring(1,4);
   System.out.println(t);

</syntaxhighlight>

The program fragment and the output are shown. State the missing value for q
Program Fragment

<syntaxhighlight lang="java"> for(int i=q; i<9; i+=3){

 System.out.print("x");

} </syntaxhighlight>

Outputxxxx

The program fragment and the output are shown. State the highest possible value for q.
Program Fragment

<syntaxhighlight lang="java"> for(int i=q; i<9; i+=3){System.out.print("x");} </syntaxhighlight>

Outputxxx

The program fragment and the output are shown. State the highest possible value for q.
Program Fragment

<syntaxhighlight lang="java"> int i = 0;
while (i++<q) {System.out.print("x");} </syntaxhighlight>

Outputxx

Evaluate

<syntaxhighlight lang="java">15-5*2</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">(1+3)*(4-2)</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">int i = 7+2*3-6/4</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">The ternary expression B ? X : Y gives X if B is true and Y if B is false.</syntaxhighlight> <syntaxhighlight lang="java">1>3?6:8</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">(2<3||6>7)?(1==1?3:4):5</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">(0==1?2>3:4>5)?6:7</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">0==1?2>3:4<5?6:7</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">The binary operator << is bit-shift-let. The expression A << B evaluates to A*2^B</syntaxhighlight> <syntaxhighlight lang="java">((1<<3)+1)<<2</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">(123<<7)/((123<<3)<<3)</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">(123<<7)/((1<<3)<<3)</syntaxhighlight>

<syntaxhighlight lang="java"> The array a is defined as: int a[] = {4,2,8,6,9,1,3,0,7,5}; </syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">a[2]+1</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">a[a[3]]</syntaxhighlight>

Evaluate

<syntaxhighlight lang="java">a[a[a[a[0]]]]</syntaxhighlight>

Give the output of the following program fragment:

<syntaxhighlight lang="java"> int x[] = {1,3,0,2}; int b = 0; for (int i=0;i<161;i++){b = x[b];} System.out.println(b); </syntaxhighlight>

The array x is defined by <syntaxhighlight lang="java"> int x[] = {1,3,0,2}; </syntaxhighlight> The array y is such that y[x[i]] == i for all values of i from 0 to 3
<syntaxhighlight lang="java">Evaluate y[0]+y[2]</syntaxhighlight>