Professional Documents
Culture Documents
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;
/* Takes in two numbers, x and b, adds them, and stores the result in the result argument */
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?
#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;
}
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?
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?
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?
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;
}
When a function parameter is a pointer, what assumption can we certainly make about that
parameter's purpose?
Which of the following is NOT a valid reason to pass a parameter by reference instead of by
value?
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;
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