You are on page 1of 11

Which of the following snippets of code best demonstrates good practice when using scanf() to

read in a long string from the user in order to prevent potential bugs, assuming the user will not
type any spaces and will be sending additional user input in future code?

A) char name[10];
scanf("%10s", name);
scanf("\n");

B) char name[10];
scanf("%9s", name);
while (getchar() != '\n');

C) char name[10];
scanf("%10s", name);
while (getchar() != '\n');

D) char name[10];
scanf("%s", name);
if (sizeof(name) > 10) {
name [9] = '\0';
while (getchar() != '\n');
}

E) char name[10];
scanf("%s", name);
scanf("\n");

Assuming the following code is written inside the main function of a C program, what would be
printed to the screen if the following code is compiled and run in a shell?

int b = 39;
printf("%5d", b);
return 0;

A) 3 spaces, then the decimal number 39


B) The decimal number 39, then 5 spaces
C) 5 spaces, then the decimal number 39
D) The decimal number 39, then 3 spaces
E) The program will not compile/execute
Given the following description and prototype of a function, what is the most likely purpose of
each parameter as input, output, or input/output?

/* Takes in two numbers, x and b, adds them, and stores the result in the result argument */

void addTogether (int* result, int x, int b)

A) out: x, out: b, in: result


B) out: x, out: b, out: result
C) in: x, in: b, in/out: result
D) out: x, out: b, in/out: result
E) in/out x, in/out b, out: result
F) in: x, in: b, in: result
G) in: x, in: b, out: result

Assuming the following code is written inside the main function of a C program, what would be
printed to the screen if the following code is compiled and run in a shell?

int var_h = 65;


printf("%c", var_h);
return 0;

A) The decimal value '65' is printed to the screen


B) The lowercase letter 'a' is printed to the screen
C) The uppercase letter 'A' is printed to the screen
D) The string '65' is printed to the screen, with single quotes
E) The program will produce a runtime error
F) The program will produce a compile time error
What is the result of trying to compile and run the following C code? Assume that if the code
would execute, the user enters the number 19 when prompted.

#include <stdio.h>

int main() {
int h;
printf("Please enter a number: ");
scanf("%d", &h);
printf("\n%d + 12 = %d\n", h, h + 12);

return 0;
}

A) The program will print the string: '19 +12=31'


B) The program will print the string: '19+12= 1912'
C) The program will print the string: "%d +12= %d h h + 12'
D) The code will produce a compile time error
E) The code will produce a runtime error

The binary value 0b11001111 is initialized as an unsigned char. Assuming that we are using the
same environment and approach used in class, what is the result of
0b11001111 >> 2?

A) 00110011
B) 00111111
C) 00111100
D) 11110011
F) 00100000

The binary value 0b10111001 is initialized as a signed char. Assuming that we are using the
same environment and approach used in class, what is the result of 0b10111001 >> 1?

A) 11011100
B) 01000000
C) 01110010
D) 01110011
E) 01011100
Given the complete binary representation in memory of a variable is given as: 00100110, what is
the MOST PRECISE assumption we can make about the variable type with complete certainty,
given that it is the default size of this type based on our discussions in class and in our course
VM environment?

A) The variable is a signed int


B) The variable is a signed char
C) The variable is either a signed int or an unsigned int
D) The variable is an unsigned char
E) The variable is either an unsigned char or an unsigned int
F) The variable is either a signed char or an unsigned char The variable is an unsigned int

Assuming the same environment as our course virtual machine, what is the binary
representation of the following variable declaration as it appears in memory, including the
correct number of bits?

unsigned int d = 0x24;

A) 00100100
B) 00100101
C) 00000000 00000000 00000000 00100101
D) 000000000 00000000 00000000 00101000
E) 00000000 00000000 00000000 00100100
F) 00101000
G) 000000000 00000000 00000000 00100000
H) 00100000

Assuming the same environment as our course virtual machine, what is the binary
representation of the following variable declaration as it appears in memory,
including the correct number of bits?

signed char a = -39;

A) 11011001
B) 00100111
C) 11011000
D) 100111
E) 011001
Which of the following pieces of code correctly assigns the string 'Brock' to the variable name,
changes the first letter to z, and prints it?

A)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char name[32];
name = "Brock";
name[0] = 'Z';
printf("%s", *name);

return 0;
}

B)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char *name;
name = "Brock";
name[0] = 'Z';
printf("%s", name);

return 0;
}

C)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char name[32];
name = "Brock";
name[0] = 'Z';
printf("%s", name);
return 0;
}

D)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char name[32];
strcpy(name, "Brock");
name[0] = 'Z';
printf("%s", name);

return 0;
}

E) All options will result in a runtime error.


F) All options will result in a compile time error

When a function parameter is a pointer, what assumption can we certainly make about that
parameter's purpose?

A) The pointer is an output parameter


B) The pointer is an input/output parameter
C) The pointer is an input parameter
D) We cannot make any of these assumptions about the pointer parameter

Which of the following is NOT a valid reason to pass a parameter by reference instead of by
value?

A) To output multiple different values from a function


B) To avoid copying large data structures
C) To obtain the binary representation of the parameter
D) To modify the value of an out-of-scope variable
Given the following array definitions, which of the provided options will print the value 3?

int vals[10];
int *valsPtr = vals + 5;
for (int i = 0; i < 10; i++) {
vals[i] = i;
}

A) printf("%d\n", *vals[3]);
B) printf("%d\n", vals + 3);
C) printf("%d\n", *(valsPtr + 2));
D) printf("%d\n", *(valsPtr - 2));
E) All options will produce a runtime error
F) All options will produce a compile time error

Which of the following pieces of code correctly passes an argument by reference to the function
hal()?

A)
int b = 9000;
hal(&b);

B)
hal(9000);

C)
int b = 9000;
hal(*b);

D)
int *b = 9000;
hal(b);

E)
int b = 9000;
hal(b);
Based on our discussions in class about how structures are organized in memory and given our
course programming environment, what would be the size of the following structure?

struct MyStruct {
float a;
char b;
};

A) 7 bytes
B) 8 bytes
C) 10 bytes
D) 6 bytes
E) 5 bytes
F) 12 bytes
G) 9 bytes
H) 1 bytes

Given the following two structures, what is the correct way to access the field identifier of the
planet field of a structure initialized as SolarSystemType system, assuming all fields have been
correctly initialized earlier in the program?

typedef struct {
char *identifier;
double distanceFromSun;
} PlanetType;

typedef struct {
PlanetType *planet;
int numberOfPlanets;
} SolarSystemType;

A) system.planet.identifier;
B) system->planet.identifier;
C) system->planet->identifier;
D) system.planet->identifier;
Based on our discussions in class about how structures are organized in memory and given our
64-bit course programming environment, what would be the size of the
structure Outer when correctly instantiated?

struct Inner {
unsigned int a;
float b;
signed int c;
int d;
float e;
int f;
};

struct Outer {
struct Inner *inner;
int g;
int h;
};

A) 56 bytes
B) 12 bytes
C) 16 bytes
D) 64 bytes

Given the following two structures, what is the correct way to access the field
alias of the author field of a structure initialized as BookType book, assuming all fields have
been correctly initialized earlier in the program?

typedef struct {
char *alias;
int yearBorn;
} AuthorType;

typedef struct {
AuthorType author;
char *title;
} BookType;

A) book.author->alias;
B) book->author.alias;
C) book->author->alias;
D) book.author.alias;
Given the following code, select the option that correctly identifies all variables stored in the data
segment.

#include <stdio.h>

double bulbasaur = 1;

void revive(int index, float count) {


float total = count - 1;
printf("You revived your pokemon, id: %d, with %f re
}

int main() {
int psyduck = 54;
revive(psyduck, 7);
return 0;
}

A) The data for bulbasaur and psyduck are stored in the data segment
B) The data for index and count are stored in the data segment
C) The data for bulbasaur, psyduck, and total are stored in the data segment
D) The data for bulbasaur is stored in the data segment.
E) The data for index, count, and total are stored in the data segment
F) The data for psyduck and total are stored in the data segment

You might also like