testc

advertisement
1. The statement that correctly defines an integer called sum is
A) sum : integer;
C) int sum;
B) integer sum;
D) sum int;
2. The statement that correctly defines a character called letter is
A) letter := char;
C) letter : char;
B) char letter;
D) character letter;
3. The correct definition for a variable called money which can
be used to hold currency, is
A) money : real;
C) float money;
B) real money;
D) money float;
4. The correct definition of a variable called arctan which will
hold scientific notation values (+e), is
A) arctan : float;
C) double arctan;
B) real arctan;
D) arctan float;
5. The correct definition of an integer variable called total
initialized to zero, is
A) total : integer = 0;
C) int total = 0;
B) total = 0, int;
D) int = 0, total;
6. The correct define statement for a constant called GST with a
value of .125, is
A) #define GST 0.125
C) float GST=0.125;
B) GST .125;
D) #define GST .125;
7. The statement that correctly assigns the sum of the two
variables loop_count and petrol_cost to the variable sum, is
A) loop_count = sum + petrol_cost;
B) petrol_cost = sum - loop_count;
C) sum = petrol_cost / loop_count;
D) sum = loop_count + petrol_cost;
8. The correct statement which assigns the character W to the
char variable letter, is
A) letter = "W";
C) char letter = "W";
B) letter = 'W';
D) strcpy( letter, "W" );
9. The correct statement which assign the decimal result of
dividing the integer variable sum by 3 into the float variable
costing, is (Use type casting to ensure that floating point
division is performed)
Given: int sum = 7; float costing;
A) (float) costing = sum / 3;
C) costing = (float) sum / 3;
B) costing = (float) (sum / 3);
D) costing = float ( sum / 3 );
10.The statement which prints out the text string "Welcome",
followed by a newline, is.
A) printf("Welcome\n");
C) printf(Welcome\n);
B) printf(Welcome, '\n');
D) printf('Welcome', '\n');
11. The statement which prints out the value of the float variable
discount, is
A) printf("%s", discount);
C) printf("%f", discount);
B) print("%f", &discount);
D) printf("%d", discount)
12. The statement to read a decimal value from the keyboard,
into the integer variable sum, is
A) scanf("%d", &sum);
C) scanf("%s", sum);
B) scanf(sum);
D) scanf("%f", &sum);
13. The statement to read a single character from the keyboard
into the variable operator, skipping leading blanks, tabs and
newline characters, is
A) scanf("%s", operator);
C) scanf("%c", operator);
B) scanf("%c", &operator);
D) scanf(" %c", &operator);
14. The statement which prints out the values 1 to 10 on
separate lines, is
A)
for( count = 1; count <= 10; count = count + 1)
printf("%d\n", count);
B)
for( count = 1; count < 10; count = count + 1)
printf("%d\n", count);
C)
for( count = 0; count <= 9; count = count + 1)
printf("%d ", count);
D)
for( count = 1; count <> 10; count = count + 1)
printf("%d\n", count);
15. The statement which produces the following output is, (hint:
use two nested for loops)
1
22
333
4444
55555
A)
for(a = 1; a <= 5; a = a + 1) {
for( b = 1; b <= 5; b = b + 1)
printf("%d", b);
printf("\n"); }
B)
for( a = 1; a <= 5; a = a + 1) {
for( b = 1; b <= a; b = b + 1)
printf("%d", a);
printf("\n"); }
C)
for( a = 1; a <= 5; a = a + 1) {
for( b = a; b <= 5; b = b + 1)
printf("%d", b);
printf("\n"); }
D)
for( a = 1; a <= 5; a = a + 1) {
for( b = 1; b < a; b = b + a)
printf("%d", b);
printf("\n"); }
16. The statement which sums all values between 10 and 100
into a variable called total is, assuming that total has NOT been
initialized to zero.
A) for( a = 10; a <= 100; a = a + 1)
total = total + a;
B)
for( a = 10; a < 100; a = a + 1, total = 0)
total = total + a;
C)
for( a = 10; a <= 100, total = 0; a = a + 1)
total = total + a;
D)
for( a = 10, total = 0; a <= 100; a = a + 1)
total = total + a;
17. The statement that prints out the character set from A-Z, is
A)
for( a = 'A'; a < 'Z'; a = a + 1)
printf("%c", a);
B)
for( a = 'a'; a <= 'z'; a = a + 1)
printf("%c", &a);
C)
for( a = 'A'; a <= 'Z'; a = a + 1)
printf("%c", a);
D)
for( a = 'Z'; a <= 'A'; a = a + 1)
printf("%c", a);
18. The statement which reproduces the following output, is
1
22
333
4444
55555
A)
a = 1;
while( a <= 5 ) {
while( b <= a ) {
printf("%d\n", a);
b = b + 1; }
a = a + 1; }
B)
a = 1;
while( a <= 5 ) {
b = 1;
while( b <= a ) {
printf("%d", a);
b = b + 1; }
printf("\n");
a = a + 1; }
C)
a = 1;
while( a <= 5 ) {
while( b <= 5 ) {
printf("%d", a);
b = b + 1; }
a = a + 1;
printf("\n"); }
D)
a = 1;
while( a <= 5 ) {
printf("\n");
b = 1;
while( a <= b ) {
printf("%d", a);
b = b + 1; }
a = a + 1; }
19. The statement that compares total for equality to
good_guess, and if equal prints the value of total, and if not
equal prints the value of good_guess, is
A)
if( total < good_guess )
printf("%d", total );
else
printf("%d", good_guess );
B)
if( total == good_guess )
printf("%d", good_guess );
else
printf("%d", total );
C)
if( total = good_guess )
printf("%d", total );
else
printf("%d", good_guess );
D)
if( total == good_guess )
printf("%d", total );
else
printf("%d", good_guess );
20. if flag is 1 or letter is not an 'X', then assign the value 0 to
exit_flag, else set exit_flag to 1.
A) if( (flag = 1) || (letter != 'X') )
exit_flag = 0;
else
exit_flag = 1;
B) if( (flag == 1) || (letter <> 'X') )
exit_flag = 0;
else
exit_flag = 1;
C) if( (flag == 1) || (letter != 'X') )
exit_flag = 0;
else
exit_flag = 1;
21. rewrite the following statements using a switch statement
if( letter == 'X' )
sum = 0;
else if ( letter == 'Z' )
valid_flag = 1;
else if( letter == 'A' )
sum = 1;
else
printf("Unknown letter -->%c\n", letter );
A) switch( letter ) {
case 'X' : sum = 0; break;
case 'Z' : valid_flag = 1; break;
case 'A' : sum = 1; break;
default : printf( "Unknown letter -->%c\n", letter ); break; }
B) switch( letter ) {
case 'X' : sum = 0;
case 'Z' : valid_flag = 1;
case 'A' : sum = 1;
default : printf( "Unknown letter -->%c\n", letter ); }
C) switch( letter ) {
case "X" : sum = 0; break;
case "Z" : valid_flag = 1; break;
case "A" : sum = 1; break;
default : printf( "Unknown letter -->%c\n", letter ); break; }
22. The statement which declares a character based array called
letters of ten elements is,
A) letters : char[10];
B) char[10] letters;
C) char letters[10];
D) char array letters[0..9];
23. Use a for loop to total the contents of an integer array called
numbers which has five elements. Store the result in an integer
called total.
A) for( loop = 0, total = 0; loop >= 4; loop++ )
total = total + numbers[loop];
B) for( loop = 0, total = 0; loop < 5; loop++ )
total = total + numbers[loop];
C) for( loop = 0, total = 0; loop <= 5; loop++ )
total = total + numbers[loop];
24. Declare a multidimensioned array of floats called balances
having three rows and five columns.
A) float balances[3][5];
B)balances[3][5] of float;
C) float balances[5][3];
D) array of floatbalances[0..2][0..5];
25. Write a for loop to total the contents of the
multidimensioned float array balances, as declared in question
24.
A) for( row = 0, total = 0; row < 3; row++ )
for( column = 0, total = 0; column < 5; column++ )
total = total + balances[row][column];
B) for( row = 0, total = 0; row < 3; row++ )
for( column = 0; column < 5; column++ )
total = total + balances[row][column];
C) for( row = 0, total = 0; row < 3; row++ )
for( column = 0; column < row; column++ )
total = total + balances[row][column];
26. Assign the text string "Hello" to the character based array
words at declaration time.
A) char words[10] = 'Hello';
B) static char words[] = "Hello";
C) static char words["hello"];
D) static char words[] = { Hello };
27. Assign the text string "Welcome" to the character based
array stuff (not at declaration time)
A) strcpy( stuff, 'Welcome' );
B) stuff = "Welcome";
C) stuff[0] = "Welcome";
D) strcpy( stuff, "Welcome" );
28. Use a printf statement to print out the third element of an
integer array called totals
A) printf("%d\n", &totals[3] );
B) printf("%d\n", totals[3] );
C) printf("%c\n", totals[2] );
D) printf("%d\n", totals[2] );
29. Use a printf statement to print out the contents of the
character array called words
A) printf("%s\n", words);
B) printf("%c\n", words);
C) printf("%d\n", words);
D) printf("%s\n", words[2]);
30. Use a scanf statement to read a string of characters into the
array words.
A) scanf("%s\n", words);
B) scanf(" %c", words);
C) scanf("%c", words);
D) scanf("%s", words);
31. Write a for loop which will read five characters (use scanf)
and deposit them into the character based array words,
beginning at element 0.
A) for( loop = 0; loop < 5; loop++ )
scanf("%c", &words[loop] );
B) for( loop = 0; loop <= 5; loop++ )
scanf("%c", words );
C) for( loop = 0; loop < 5; loop++ )
scanf("%c", &words[0] );
32. A function called print which prints a text str passed to it as
a parameter (ie, a character based array), looks like
A) int print( char str[ ] ) {
printf("%s", str);
}
B) void print( char str[ ] ) {
printf("Menu choices");
}
C) void print( char str[ ] ) {
printf("%s", str);
}
33. A function called total, totals the sum of an integer array
passed to it (as the first parameter) and returns the total of all the
elements as an integer. Let the second parameter to the function
be an integer which contains the number of elements of the
array.
A) int total( int numbers[], int elements ) {
int total = 0, loop;
for( loop = 0; loop < elements; loop++ )
total = total + numbers[loop];
return total;
}
B) int total( int numbers[], int elements ) {
int total = 0, loop;
for( loop = 0; loop <= elements; loop++ )
total = total + numbers[loop];
return total;
}
C) int total( int numbers[], int elements ) {
int total, loop;
for( loop = 0; loop > elements; loop++ )
total = total + numbers[loop];
return total;
}
34. A structure called rec which holds an integer called loop, a
character array of 5 elements called words, and a float called
sum, looks like
A) struct rec {
int loop;
char words[5];
float sum;
};
B) type structure rec {
loop : integer;
words : array[0..4] of char;
sum : real;
};
C) type record {
integer loop;
char words[4];
float sum;
}
35. The statement which declares a structure variable called
sample, defined from a structure of type struct rec, is
A) type sample : rec;
B) struct sample;
C) struct rec sample;
D) declare sample as type rec;
36. The statement that assigns the value 10 to the field loop of
the sample structure (which is of type struct rec), is
A) loop = 10;
B) sample.loop = 10;
C) rec.sample.loop = 10;
D) rec.loop = 10;
37. The statement that prints out (using printf) the value of the
words array of the sample structure is
A) printf("%d", sample);
B) printf("%s", words );
C) printf("%c", sample-words ); D)printf("%s",sample.words );
38. The correct definition for a structure called birthdays, whose
fields are a structure of type struct time called btime, and a
structure of type struct date, called bdate, is
A) birthdays {
time btime;
date bdate; };
B) struct birthdays {
struct time btime;
struct date bdate; };
C) struct birthdays {
struct bdate date;
struct btime time; };
39. The statement that defines an input file handle called
input_file, which is a pointer to type FILE, is
A) type input_file as FILE;
B) FILE *input_file;
C) input_file FILE;
D) *FILE input_file;
40. Using input_file, open the file results.dat for read mode.
A) input_file = "results.dat" opened as "r";
B) open input_file as "results.dat" for "r";
C) fopen( input_file, "results.dat", "r" );
D) input_file = fopen( "results.dat", "r" );
41. Write C statements which tests to see if input_file has
opened the data file successfully. If not, print an error message
and exit the program.
A) if( input_file == NULL ) {
printf("Unable to open file.\n");\
exit(1);
}
B) if( input_file != NULL ) {
printf("Unable to open file.\n");\
exit(1);
}
C) while( input_file = NULL ) {
printf("Unable to open file.\n");\
exit(1);
}
42. Write C code which will read a line of characters
(terminated by a \n) from input_file into a character array called
buffer. NULL terminate the buffer upon reading a \n.
A)
int ch, loop = 0;
ch = fgetc( input_file );
while( (ch != '\n') && (ch != EOF) ) {
buffer[loop] = ch;
loop++;
ch = fgetc( input_file );
}
buffer[loop] = NULL;
B)
int ch, loop = 0;
ch = fgetc( input_file );
while( (ch = '\n') && (ch = EOF) ) {
buffer[loop] = ch;
loop--;
ch = fgetc( input_file );
}
buffer[loop] = NULL;
C)
int ch, loop = 0;
ch = fgetc( input_file );
while( (ch <> '\n') && (ch != EOF) ) {
buffer[loop] = ch;
loop++;
ch = fgetc( input_file );
}
buffer[loop] = -1;
43. Close the file associated with input_file.
A) close input_file;
B) fclose( input_file );
C) fcloseall();
D) input_file( fclose );
44. Declare a pointer to an integer called address.
A) int address;
B) address *int;
C) int *address;
D) *int address;
45. Assign the address of a float variable balance to the float
pointer temp.
A) temp = &balance;
B) balance = float temp;
C) float temp *balance;
D) &temp = balance;
46. Assign the character value 'W' to the variable pointed to by
the char pointer letter.
A) 'W' = *letter;
B) letter = "W";
C) letter = *W;
D) *letter = 'W';
47. What is the output of the following program segment?
int count = 10, *temp, sum = 0;
temp = &count;
*temp = 20;
temp = ∑
*temp = count;
printf("count = %d, *temp = %d, sum = %d\n", count,
*temp, sum );
A) count = 2, *temp = 10, sum = 10
B) count = 20, *temp = 20, sum = 20
C) count = 10, *temp = 2, sum = 10
D) count = 200, *temp = 0.2, sum = 1
48. Declare a pointer to the text string "Hello" called message.
A) char message = "Hello";
B) *message = "Hello";
C) char *message = "Hello";
D) char message = 'Hello';
49. Declare a pointer to a structure of type date called dates.
A) struct dates dates;
B) struct *date *dates;
C) struct dates date;
D) struct date *dates;
50. If the above structure of type date comprises three integer
fields, day, month, year, assign the value 10 to the field day
using the dates pointer.
A) dates.day = 10;
B) dates->day = 10;
C) dates = 10.day;
D) day.dates = 10;
51. A structure of type machine contains two fields, an integer
called name, and a char pointer called memory. Show what the
definition of the structure looks like.
A) struct machine {
int name;
char memory; }
B) machine {
name : integer;
memory : char^; };
C) struct machine {
int name;
char *memory; };
52. A pointer called mpu641 of type machine is declared. What
is the command to assign the value NULL to the field memory.
A) mpu641->memory = (char *) NULL;
B) mpu641.memory = 0;
C) mpu641-memory = 0;
D) strcpy( mpu641.memory, NULL);
53. Assign the address of the character array CPUtype to the
field memory using the pointer mpu641.
A) mpu641.memory = &CPUtype;
B) mpu641->memory = CPUtype;
C) strcpy( mpu641.memory, CPUtype);
D) mpu641.memory = CPUtype;
54. A structure pointer times of type time (which has three
fields, all pointers to integers, day, month and year respectively)
is declared. Using the pointer times, update the field day to 10.
A) times.day = 10;
B) *(times->day) = 10;
C) *times.day = 10;
D) times.day = *10;
55. An array of pointers (10 elements) of type time (as detailed
above in 54.), called sample is declared. Update the field month
of the third array element to 12.
A) *(sample[2]->month) = 12;
B) sample[3].month = 12;
C) *sample[2]->month = 12;
D) *(sample[3]->month) = 12;
Download