Title: Review
1Review
- Selection structures
- if, if-else, and switch statements
- Boolean values and Boolean expression
- Repetition structures
- for loops and while loops
- Indexed collections/structured data
- vectors and arrays
- Search and sort
- linear search and binary search
- selection sort and bubble sort
- time complexity
2Sample Questions
- Which of the following loop statements will
execute the body at least once? - For loops
- while loops
- do-while loops
- none of the above
- Which of the following will certainly cause
compiler error - String students new String
- students5 new String(Bill Bradley)
- String students new String()
- String students
3- How many lines will the following code print out?
- 25
- 20
- 16
- 30
- You are asked to write a method to compute the
average mark from a list of marks to be typed in
from the keyboard. Which of the following local
variables are suitable for the method - int marks int total
- Integer marks int total
- Vector marks
- int account, total
for ( int out 0 out lt 5 out )
for ( int inner 4 inner gt 0 inner--)
System.out.println(----------------)
4- Write a method to compute the average mark of a
given course. - (1) your method should take an int array mark
as an input, and - (2) the method should return a float number
float getAverageMark ( int marks )
float total total 0 for ( int
index0 index lt marks.length index)
total total marksindex return (
total / marks.length )
5- Write a method to print out a pattern as shown
below. The method, called displaySarArray, takes
one int parameter size, and prints out a matrix
with size rows and size columns such that the
upper-right, lower-left, and diagonal of the
matrix contain 0, , and 1 respectively. A
sample of displayStarArray(5) is shown below.
1 0 0 0 0 1 0 0 0 1 0
0 1 0 1
6Void displayStarArray( int size ) for (
int row 1 row lt size row ) for
( int column 1 column ltsize column )
if ( row lt column )
System.out.print( 0) else if
( row column )
System.out.print( 1) else
System.out.print( )
System.out.println()
7- Write a method to bubble sort a given array of
ints in descending order.
public static void bubble_sort( int data )
int j, k, temp for ( j 0 j lt
data.length -2 j ) for ( k 0 k lt
data.length-2-j k ) if ( datak
lt datak1 ) temp datak
datak datak1
datak1 temp