Programming Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

C Programming Quiz: If statements

1. Which of the following is true?


A. 1
B. 66
C. .1
D. -1
E. All of the above

2. Which of the following is the boolean operator for logical-and?


A. &
B. &&
C. |
D. |&

3. Evaluate !(1 && !(0 || 1)).


A. True
B. False
C. Unevaluatable

4. Which of the following shows the correct syntax for an if statement?


A. if expression
B. if { expression
C. if ( expression )
D. expression if

Quiz: Functions

1. Which is not a proper prototype?


A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();

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();

4. Which of the following is a complete function?


A. int funct();
B. int funct(int x) {return x=x+1;}
C. void funct(int) { printf( "Hello");
D. void funct(x) { printf( "Hello"); }

Quiz : Arrays

1. Which of the following correctly declares an array?


A. int anarray[10];
B. int anarray;
C. anarray{10};
D. array anarray[10];

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

3. Which of the following is a two-dimensional array?


A. array anarray[20][20];
B. int anarray[20][20];
C. int array[20, 20];
D. char array[20];

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?

2. When does the code block following while(x<100) execute?


A. When x is less than one hundred
B. When x is greater than one hundred
C. When x is equal to one hundred
D. While it wishes

3. Which is not a loop structure?


A. For
B. Do while
C. While
D. Repeat Until

4. How many times is a do while loop guaranteed to loop?


A. 0
B. Infinitely
C. 1
D. Variable

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];

System.out.println( ar[0] + " " + ar[3] );d. 23 4


-What is the output of the following code fragment:
int[] y = new int[5];

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;

System.out.println( z[0] + z[1] + " " + z[5] ); a. 10 0


-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;

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;

System.out.println( zip[ j-1 ] ); c. 4

-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?

int sum = 14;


if ( sum < 20 )
System.out.print("Under ");
else
System.out.print("Over ");
System.out.println("the limit."); c. Under the limit.
What does the following code fragment write to the monitor?
int sum = 14;
if ( sum < 20 )
System.out.print("Under ");
else
{
System.out.print("Over ");
System.out.println("the limit.");
}

(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!)

d. Over the limit.

5. What does the following code fragment write to the monitor?


int sum = 7;
if ( sum > 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}

System.out.println("the prize.");

(Notice that the program has changed from the previous question!)

d. You lose the prize

6. What does the following code fragment write to the monitor?


int sum = 21;
if ( sum == 20 )
{
System.out.print("You win ");
}
else
{
System.out.print("You lose ");
}

System.out.println("the prize.");

(Notice that the program has changed from the previous question!)

d. You lose the prize.

7. What does the following code fragment write to the monitor?


int sum = 21;
if ( sum != 20 )
System.out.print("You win ");

else
System.out.print("You lose ");

System.out.println("the prize.");

(Notice that the program has changed from the previous question!)

c. You win the prize.

8. A sequence of statements contained within a pair of braces ("{" and "}") is called a:

a. block

9. Evaluate (to true or false) each of the following expressions:


14 <= 14 14 < 14 -9 > -25 -25 > -9

d. true false true false

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

d. false false true true

Quiz Increment and Decrement

-What is the most common operation performed by a program? a. Adding integer one to an integer variable.

-What two steps are performed when an assignment statement is executed? a.

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.

What does the following program output to the monitor:

int value = 0;

int count = 1;

value = count++ ;
System.out.println("value: "+ value " + count: " + count );

d. value: 1 count: 2

What does the following program output to the monitor:

int value = 0;

int count = 1;

value = ++count ; /* note change from previous */

System.out.println("value: "+ value " + count: " + count );

d. value: 2 count: 2

What is the output of the following:

int a = 0;

int b = 10;

a = --b ;

System.out.println("a: " + a + " b: " + b );

c. a: 9 b:9

What is the output of the following program:

double w = 12.5 ;

w *= 2 ;

System.out.println( " w is " + w );

c. w is 25.0

Which of the answers does the same thing as the following:

value += sum++ ;

value = value + sum;

sum = sum + 1;

Fill in the blank so that wages is divided by two.

wages ____________ 2 ;

d. /=
Are the auto-increment and auto-decrement operators (++ and -- ) an essential part of the Java language?

a.No---any program that uses them could be written without them.

QUIZ ON ARRAY

What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 }; c. 6

What is the output of the following code fragment:


int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

for ( int index= 0 ; index < 5 ; index++ )


System.out.print( egArray[ index ] + " " );
b. 2 4 6 8 10

What is the output of the following code fragment:


int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

for ( int index= 0 ; index < egArray.length ; index++ )


System.out.print( egArray[ index ] + " " );
d. 2 4 6 8 10 1 3 5 7 9

What is the output of the following code fragment:


int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };

for ( int index= 0 ; index < egArray.length ; index = index + 2 )


System.out.print( egArray[ index ] + " " );
c. 2 6 10 3 7

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 };

for ( int index= ________ ; _____________ ; ______________ )


System.out.print( egArray[ index ] + " " );
d. index = egArray.length-1; index >= 0; index—

Examine the following program fragment:


int[] array = { 1, 4, 3, 6, 8, 2, 5};
int what = array[0];

// scan the array


for ( int index=0; index < array.length; index++ )
{
if ( array[ index ] > what )
what = array[ index ];
}
System.out.println( what );
What does the fragment write to the monitor?
d. 8

Examine the following program fragment:


int[] array = { 1, 4, 3, 6, 8, 2, 5};
int what = array[0];

// scan the array


for ( int index=0; index < array.length; index++ )
{
if ( array[ index ] < what )
what = array[ index ];
}
System.out.println( what );
What does the fragment write to the monitor?

a. 1

Examine the following program fragment:


int[] array = { 1, 4, 3, 6 };
int what = 0;

// scan the array


for ( int index=0; index < array.length; index++ )
{
what = what + array[ index ] ;
}
System.out.println( what );
What does the fragment write to the monitor?

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];

// scan the array


for ( int index=0; index < array.length; index++ )
{
_______________________
}
d. array[ index ] = 2*index;
QUIZ ON LOOPS

What are the three general types of looping structures? a. counting loop, sentinel-controlled loop, and result-controlled
loop.

What is the output of the following program fragment?


for ( int j = 0; j < 5; j++ )

System.out.print( j + " " );

System.out.println( );

b.0 1 2 3 4

What is the output of the following code fragment?


for ( int j = 10; j > 5; j-- )

System.out.print( j + " " );

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.print( 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.print( j + " " );

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.print( j + " " );

System.out.println( );

c. int j = -3

What is the output of the following code fragment?


for ( int j = 5; j > -5; j-- )

System.out.print( j + " " );

System.out.println( );

d. 5 4 3 2 1 0 -1 -2 -3 -4

What is the output of the following code fragment?


int count = 0;

for ( ; count < 9; ++count )

System.out.print( count + " " );

System.out.println( );

a. 0 1 2 3 4 5 6 7 8

What is the output of the following code fragment?

for ( int count = 0; count < 9; )

System.out.print( count + " " );

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 ...

What is the output of the following code fragment?


int j;
for ( j = 0; j < 5; )

System.out.print( j + " " );

j++ ;

System.out.println( );

a. 0 1 2 3 4

You might also like