Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Code-A-Vita

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.
a)

The variable a is not initialized correctly.

b)

The variable b is not declared correctly.

c)

The variable c is not initialized correctly.

d)

The variable d is not declared correctly.

2.
a)

The condition in the for loop is incorrect.

b)

The semicolon ; at the end of the for loop line makes the loop body empty

c)

The printf statement is outside the loop, so it will only print number 6.

d)

The variable i is not initialized properly.

3.
a)

Syntax error in the printf statement

b)

Division by zero error

c)

Incorrect variable type declaration

d)

No error in the code

4.
a)

The loop condition is incorrect, causing an off-by-one error.

b)

There is no error; the program will always print "Sum is exactly 1.0".

c)

The comparison sum == 1.0 is incorrect due to the nature of floating-point arithmetic.

d)

The variable sum should be of type double for the summation to be accurate.

5.
a)

Change the function declaration to int sqr(int n);

b)

Change the function call in main to result = square(number);

c)

Change the function definition to int sqr(int n) { return n * n; }

d)

Change the printf statement to printf("The square of %d is %d.\n", number, square);

6.
a)

The MAX macro is incorrectly defined.

b)

The MAX macro does not need parentheses.

c)

The printf statement is incorrect.

d)

There is no error in the code.

7.
a)

The pointer p is not initialized correctly.

b)

The statement *p = a + 5; is incorrect and will not compile.

c)

There is no error in the code; it will print "The value of a is: 15".

d)

The printf statement uses the wrong format specifier.

8.
a)

The swap function incorrectly uses pointer dereferencing, leading to undefined behavior.

b)

The swap function should not use pointers to swap the values of a and b.

c)

The temp pointer is correctly initialized and used to swap the values.

d)

There is no error; the program will execute correctly and swap the values of a and b.

9.
a)

The array initialization syntax is incorrect

b)

The condition in the for loop should be i < 5 instead of i <= 5.

c)

The printf statement is missing a newline character at the end.

d)

The program has no errors.

10.
a)

Increase the size of str1 to char str1[20].

b)

Use strcpy(str1, str2) instead of strcat(str1, str2).

c)

Replace strcat(str1, str2); with printf("%s%s", str1, str2);

d)

No change needed; the code is correct.

11.
a)

The union PersonInfo cannot contain both int and float types.

b)

The favoriteColor of person1 is assigned to BLACK, which is not defined in the Color enumeration.

c)

The printf statement incorrectly tries to print the favoriteColor enumeration as an integer.

d)

The structure Person should not contain a union type as its member.

12.
a)

The scanf function should use %s instead of %c to read a character.

b)

The printf function cannot print characters using %c.

c)

There is no error in the code; it should work as expected.

d)

The program is missing #include

13.
a)

The program correctly checks the number of command line arguments, but fails to convert the arguments from strings to integers before adding them.

b)

The printf statement for displaying the sum is incorrect because it uses %d instead of %s for the sum variable.

c)

The condition argc != 3 is incorrect because it should be argc != 2 to ensure two command line arguments are passed.

d)

There is no error in the program; it correctly calculates and prints the sum of two numbers passed as command line arguments.

14.
a)

Shifting to the left by -1 is an incorrect operation, as shift values should be non negative.

b)

The left shift operation is correctly applied, but it should have been a right shift to double the value of x.

c)

The use of an unsigned int for x is incorrect; it should be a signed int to perform shift operations.

d)

There is no mistake; the code will correctly double the value of x.

15.
a)

The code is correct as is and does not require any modifications.

b)

Change r1.move(r1.pos); to r1.move(&r1.pos); because move is a function pointer that expects a pointer to coordinates.

c)

Change typedef void (*operation)(coordinates* c); to typedef void (*operation)(coordinates c); to match the function call r1.move(r1.pos);.

d)

Add #include <stdlib.h> at the beginning of the code because dynamic memory allocation is used.

16.
a)

The code will compile successfully and print "100", because the updateValue function updates the value of value to 100.

b)

The code will compile successfully but will print "30" because the const qualifier in updateValue function's parameter prevents modification of the original value.

c)

The code will not compile because of an attempt to modify a const qualified type within the updateValue function.

d)

The code will compile successfully but may cause a runtime error due to attempting to modify a constant value.

17.
a)

The error is in arr = malloc(n * sizeof(int));

b)

The error is in if (arr == NULL) {

c)

The error is in free(arr);

d)

There is no error in the memory allocation process;

18.
a)

The program is correct and does not need any changes.

b)

Change int sum = 0; to double sum = 0; to correctly calculate the average when dividing.

c)

Change the loop condition in findAverage from i <= count to i < count to avoid an extra iteration that reads beyond the provided arguments.

19.

Consider the following declarations in C. Which one of these is correctly declared and will compile without error assuming all necessary headers are included?

a)

int function(float arg); float arg;

b)

float function(float arg[]) { return arg[0]; }

c)

int (*function(float))[5];

d)

float function[] (int, float);

20.
a)

The strlen function is used incorrectly.

b)

The printf function's format specifier for string length is incorrect.

c)

The gets function should not be used to read strings.

d)

There is no error in the code.