Programming Questions
Programming Questions
Quiz: Functions
2. What is the return type of the function with prototype: "int func(char x, float v, double t);"
A. char
B. int
C. float
D. double
3. Which of the following is a valid function call (assuming the function exists)?
A. funct;
B. funct x, y;
C. funct();
D. int funct();
Quiz : Arrays
2. What is the index number of the last element of an array with 29 elements?
A. 29
B. 28
C. 0
D. Programmer-defined
4. Which of the following correctly accesses the seventh element stored in foo, an array with 100
elements?
A. foo[6];
B. foo[7];
C. foo(7);
D. foo;
5. Which of the following gives the memory address of the first element in array foo, an array with
100 elements?
A. foo[0];
B. foo;
C. &foo;
D. foo[1];
Quiz:Loops
1. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
A. 10
B. 9
C. 0
D. 1
Note: This quiz question probably generates more email to the webmaster than any other single item
on the site. Yes, the answer really is 10. If you don't understand why, think about it this way: what
condition has to be true for the loop to stop running?
QUIZ ARRAY
-Which of the following declares an array of int named img? b. int[] img;
-What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 } a. 0, 1, 2, 3
-What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
System.out.println( ar[0] + " " + ar[1] ); c. 2 4
-What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
ar[0] = 23;
ar[3] = ar[1];
y[0] = 34;
y[1] = 88;
System.out.println( y[0] + " " + y[1] + " " + y[5] ); c. The program is defective and will not
compile.
-What is the output of the following code fragment:
int[] z = new int[9];
z[0] = 7;
z[1] = 3;
z[2] = 4;
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
System.out.println( zip[ 2 + 1 ] ); d. 1
-What is the output of the following code fragment:
int[] zip = new int[5];
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
int j = 3;
-How many objects are present after the following code fragment has executed?
double[] ann = new double[ 7 ];
double[] bob;
bob = ann; d. 1
For which of the following applications is an array NOT suitable: b. Holding the name, social
security number, age, and income of one individual.
QUIZ IF STATEMENT
-How many choices are possible when using a single if-else statement? b. 2
-What does the following code fragment write to the monitor?
(Notice that the program has changed from the previous question!)
a. Under
4. What does the following code fragment write to the monitor?
int sum = 94;
if ( sum < 20 )
{
System.out.print("Under ");
System.out.println("the limit.");
}
else
{
System.out.print("Over ");
System.out.println("the limit.");
}
(Notice that the program has changed from the previous question!)
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
else
System.out.print("You lose ");
System.out.println("the prize.");
(Notice that the program has changed from the previous question!)
8. A sequence of statements contained within a pair of braces ("{" and "}") is called a:
a. block
10. Say that value has a 19 stored in it, and that extra has a 25 stored in it. Evaluate (to true or false)
each of the following expressions:
value <= extra extra < value value > -25 value >= extra
-What is the most common operation performed by a program? a. Adding integer one to an integer variable.
The expression on the right of the "=" is evaluated, "using" all the variables it contains.
The result of evaluation is assigned to the variable on the left of the "=".
What is the meaning of variable++ ? b. Add one to the variable after its current value has been used.
int value = 0;
int count = 1;
value = count++ ;
System.out.println("value: "+ value " + count: " + count );
d. value: 1 count: 2
int value = 0;
int count = 1;
d. value: 2 count: 2
int a = 0;
int b = 10;
a = --b ;
c. a: 9 b:9
double w = 12.5 ;
w *= 2 ;
c. w is 25.0
value += sum++ ;
sum = sum + 1;
wages ____________ 2 ;
d. /=
Are the auto-increment and auto-decrement operators (++ and -- ) an essential part of the Java language?
QUIZ ON ARRAY
What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 }; c. 6
Does a programmer always know how long an array will be when the program is being written?
b. No---the array object is created when the program is running, and the length might change
from run to run.
Fill in the blanks of the following code fragment so that the elements of the array are printed in
reverse order, starting with the last element.
int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
a. 1
a. 14
Fill in the blank in the following code fragment so that each element of the array is assigned twice
the value of its index.
int[] array = new int[10];
What are the three general types of looping structures? a. counting loop, sentinel-controlled loop, and result-controlled
loop.
System.out.println( );
b.0 1 2 3 4
System.out.println( );
d. 10 9 8 7 6
What must the test be so that the following fragment prints out the integers 5 through and
including 15?
for ( int j = 5; ________ ; j++ )
System.out.println( );
c. j<16
What must the change be so that the following fragment prints out the even integers 0 2 4 6 8
10?
for ( int j = 0; j <= 10; _______ )
System.out.println( );
b. j = j+2
What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?
for ( _______; j < 0; j++ )
System.out.println( );
c. int j = -3
System.out.println( );
d. 5 4 3 2 1 0 -1 -2 -3 -4
System.out.println( );
a. 0 1 2 3 4 5 6 7 8
count++ ;
System.out.println( );
d. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
j++ ;
System.out.println( );
a. 0 1 2 3 4