Difference between revisions of "Java Test 1"

From Progzoo
Jump to: navigation, search
 
(10 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
     a = a + b;
 
     a = a + b;
 
     b = a + b;
 
     b = a + b;
 +
    System.out.println(b);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<div class="i" data-regex="^\s*19\s*$" data-regex_flags="g"></div>
 
<div class="i" data-regex="^\s*19\s*$" data-regex_flags="g"></div>
Line 27: Line 28:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
     String s = "Fourty two";
 
     String s = "Fourty two";
     String t = s.substring(7,8)+substring(1,4);
+
     String t = s.substring(7,8)+s.substring(1,4);
 
     System.out.println(t);
 
     System.out.println(t);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 39: Line 40:
 
<td>
 
<td>
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
for(int i=q; i<9; i+=3)
+
for(int i=q; i<9; i+=3){
&nbsp;&nbsp;System.out.print("x");
+
  System.out.print("x");
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</td>
 
</td>
Line 54: Line 56:
 
<th>Program Fragment</th>
 
<th>Program Fragment</th>
 
<td>
 
<td>
for(int i=q; i<9; i+=3)<br/>
+
<syntaxhighlight lang="java">
  System.out.print("x");
+
for(int i=q; i<9; i+=3){System.out.print("x");}
 +
</syntaxhighlight>
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 68: Line 71:
 
<th>Program Fragment</th>
 
<th>Program Fragment</th>
 
<td>
 
<td>
 +
<syntaxhighlight lang="java">
 
int i = 0;<br />
 
int i = 0;<br />
while (i++<q)
+
while (i++<q) {System.out.print("x");}
  System.out.print("x");
+
</syntaxhighlight>
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 160: Line 164:
 
</div>
 
</div>
  
<div class='q'>The array y is such that y[x[i]] == i for all values of i from 0 to 3 <br/>
+
<div class='q'>
 +
The array x is defined by
 +
<syntaxhighlight lang="java">
 +
int x[] = {1,3,0,2};
 +
</syntaxhighlight>
 +
The array y is such that <code>y[x[i]] == i</code> for all values of i from 0 to 3 <br/>
 
<syntaxhighlight lang="java">Evaluate y[0]+y[2]</syntaxhighlight>
 
<syntaxhighlight lang="java">Evaluate y[0]+y[2]</syntaxhighlight>
 
<!--
 
<!--
Line 177: Line 186:
 
}  
 
}  
 
-->
 
-->
<div class="i" data-regex="^\s*1\s*$" data-regex_flags="g"></div>
+
<div class="i" data-regex="^\s*5\s*$" data-regex_flags="g"></div>
 
</div>
 
</div>

Latest revision as of 09:48, 28 September 2015

State the output from this program fragment:
    int a = 5;
    int b = 7;
    a = a + b;
    b = a + b;
    System.out.println(b);
State the output from this program fragment:
    int a = 5;
    int b = 7;
    int c = 8;
    a = b;
    b = c;
    c = a;
    System.out.println(100*a+10*b+c)
State the output from this program fragment:
    String s = "Fourty two";
    String t = s.substring(7,8)+s.substring(1,4);
    System.out.println(t);
The program fragment and the output are shown. State the missing value for q
Program Fragment
for(int i=q; i<9; i+=3){
  System.out.print("x");
}
Outputxxxx

The program fragment and the output are shown. State the highest possible value for q.
Program Fragment
for(int i=q; i<9; i+=3){System.out.print("x");}
Outputxxx

The program fragment and the output are shown. State the highest possible value for q.
Program Fragment
int i = 0;<br />
while (i++<q) {System.out.print("x");}
Outputxx

Evaluate
15-5*2
Evaluate
(1+3)*(4-2)
Evaluate
int i = 7+2*3-6/4
Evaluate
The ternary expression B ? X : Y gives X if B is true and Y if B is false.
1>3?6:8
Evaluate
(2<3||6>7)?(1==1?3:4):5
Evaluate
(0==1?2>3:4>5)?6:7
Evaluate
0==1?2>3:4<5?6:7
Evaluate
The binary operator << is bit-shift-let. The expression A << B evaluates to A*2^B
((1<<3)+1)<<2
Evaluate
(123<<7)/((123<<3)<<3)
Evaluate
(123<<7)/((1<<3)<<3)
The array a is defined as:
int a[] = {4,2,8,6,9,1,3,0,7,5};
Evaluate
a[2]+1
Evaluate
a[a[3]]
Evaluate
a[a[a[a[0]]]]
Give the output of the following program fragment:
int x[] = {1,3,0,2};
int b = 0;
for (int i=0;i<161;i++){b = x[b];}
System.out.println(b);

The array x is defined by

int x[] = {1,3,0,2};

The array y is such that y[x[i]] == i for all values of i from 0 to 3

Evaluate y[0]+y[2]