csc101 finals special slideshow reviewer part 2.pdf
TRANSCRIPT
-
A few reminders
-
Try to solve each problem on your own.
Help out a friend if they dont understand.
Ask if something is not clear.
Wait for somebody to answer for you.
Forget your basics.
Forget your loopings, arrays, if-elses, and switches.
-
Indexes start at 0.
0 starts at 48, A starts at 65, a starts at 97.
Numbers < Capital Letters < Small Letters.
A letter is smaller than the one next to it i.e.:
A < B < C < a < b < c
Indexes start at 0.
Counting of characters start at 1.
Null terminator \0 at the end.
-
What is the value returned if we invoke fun(11);?
Answer:
int fun(int x)
{
int result;
if (x == 0)
result = 0;
else if ((x % 2) == 0)
result = fun(x / 2);
else
result = 1 + fun(x / 2);
return result;
}
-
What is the value returned if we invoke fun(11);?
Answer:
3
int fun(int x)
{
int result;
if (x == 0)
result = 0;
else if ((x % 2) == 0)
result = fun(x / 2);
else
result = 1 + fun(x / 2);
return result;
}
-
What is the outputof the code?
Answer:
#include
int somefun(int x, int y);
int somefun(int x, int y)
{
int answer;
if(y == 0)
answer = 1;
else
answer = x*somefun(x,y-1);
return answer;
}
int main()
{
printf("%d", somefun(3,3));
return 0;
}
-
What is the outputof the code?
Answer:27
#include
int somefun(int x, int y);
int somefun(int x, int y)
{
int answer;
if(y == 0)
answer = 1;
else
answer = x*somefun(x,y-1);
return answer;
}
int main()
{
printf("%d", somefun(3,3));
return 0;
}
-
What is the
function trying to do?
Answer:
int foo(char str[], char c)
{
int answer;
if(str[0] == '\0')
answer = 0;
else if (str[0] == c)
answer = 1+foo(&str[1], c);
else
answer = foo(&str[1], c);
return answer;
}
-
What is the
function trying to do?
Answer:
It counts the number of char cs in string str.
int foo(char str[], char c)
{
int answer;
if(str[0] == '\0')
answer = 0;
else if (str[0] == c)
answer = 1+foo(&str[1], c);
else
answer = foo(&str[1], c);
return answer;
}
-
What is the
function trying to do?
Answer:
int bar(char str[])
{
int answer;
if(str[0] == '\0')
answer = 0;
else
answer = 1 + bar(&str[1]);
return answer;
}
-
What is the
function trying to do?
Answer:
It returns the length of string str.
int bar(char str[])
{
int answer;
if(str[0] == '\0')
answer = 0;
else
answer = 1 + bar(&str[1]);
return answer;
}
-
Answer:
Given the definitions of functions foo() and
bar() in Problem#3 and #4, what will be the
output after the execution of the following code
snippet?
char title[] = "PrObLeMsOlViNgAnDPrOgRaMdEsIgNiNc";
printf("Foo = %d\nBar = %d",bar(title),foo(title,'i'));
-
Answer:
Foo = 33
Bar = 2
Given the definitions of functions foo() and
bar() in Problem#3 and #4, what will be the
output after the execution of the following code
snippet?
char title[] = "PrObLeMsOlViNgAnDPrOgRaMdEsIgNiNc";
printf("Foo = %d\nBar = %d",bar(title),foo(title,'i'));
-
What is the output displayed if we invoke fun(2);?
Answer:
int fun(int x)
{
printf("%di ", x);
int result;
if (x == 0)
result = 0;
else
result = x + fun(x - 1);
printf("%do ", x);
return result;
}
-
What is the output displayed if we invoke fun(2);?
Answer:
2i 1i 0i 0o 1o 2o
int fun(int x)
{
printf("%di ", x);
int result;
if (x == 0)
result = 0;
else
result = x + fun(x - 1);
printf("%do ", x);
return result;
}
-
What is the output displayed if we invoke MysterySeq(10);?
Answer:
void MysterySeq(int n)
{
if (n == 0)
return;
else if (n%2 == 0)
n = n*3 + 1;
else
n = n / 2;
MysterySeq(n);
printf("%d ", n);
}
-
What is the output displayed if we invoke MysterySeq(10);?
Answer:
0 1 3 7 15 31
void MysterySeq(int n)
{
if (n == 0)
return;
else if (n%2 == 0)
n = n*3 + 1;
else
n = n / 2;
MysterySeq(n);
printf("%d ", n);
}
-
What is the
function trying to do?
Answer:
void funA(int n)
{
if(n == 0) return;
else
{
printf("%d", n%10);
n /= 10;
funA(n);
}
}
-
What is the
function trying to do?
Answer:
It prints the number n whose digits are in reverse order.
void funA(int n)
{
if(n == 0) return;
else
{
printf("%d", n%10);
n /= 10;
funA(n);
}
}
-
What is the
function trying to do?
Answer:
int funB(int n)
{
int digit = 0;
if(n == 0) return 0;
else
{
digit = n%10;
n /= 10;
return digit + funB(n);
}
}
-
What is the
function trying to do?
Answer:
It returns the sum of the digits of number n.
int funB(int n)
{
int digit = 0;
if(n == 0) return 0;
else
{
digit = n%10;
n /= 10;
return digit + funB(n);
}
}
-
Answer:
#include
void recFoo(int n, int m, char *str)
{
if(n==(m-1))
printf("%c", *(str+n));
else
recFoo(n+1, m, str);
printf("%c", *(str+n));
}
int main()
{
char bar[] = "Problem Solving and Program Design in C";
recFoo(20, 34, bar);
return 0;
}
-
Answer:
ngiseD margorP
#include
void recFoo(int n, int m, char *str)
{
if(n==(m-1))
printf("%c", *(str+n));
else
recFoo(n+1, m, str);
printf("%c", *(str+n));
}
int main()
{
char bar[] = "Problem Solving and Program Design in C";
recFoo(20, 34, bar);
return 0;
}
-
strlen(str)
strcpy(var, str)
strncpy(var, str, n)
strcmp(str1, str2)
strncmp(str1, str2, n)
strcat(str1,str2)
strncat(str1,str2,n)
strlwr(str)
strupr(str)
toupper(char)
tolower(char)
7 basic operations
4 additional
-
What is output of the program?
Answer:
#include
#include
int main()
{
char name1[100] = "jone ";
char name2[100] = "few jr";
char full[100] = "user ";
full = strncat(name1,name2,3);
name1[3] = '\0';
printf("%s", full);
return 0;
}
-
What is output of the program?
Answer:
No output.
Compiler Error.
#include
#include
int main()
{
char name1[100] = "jone ";
char name2[100] = "few jr";
char full[100] = "user ";
full = strncat(name1,name2,3);
name1[3] = '\0';
printf("%s", full);
return 0;
}
-
What is output of the program?
Answer:
#include
#include
int main()
{
char name1[100] = "jone ";
char name2[100] = "few jr";
char full[100] = "user ";
strcpy(full,strncat(name1,name2,
3));
name1[3] = '\0';
printf("%s", full);
return 0;
}
-
What is output of the program?
Answer:
jone few
#include
#include
int main()
{
char name1[100] = "jone ";
char name2[100] = "few jr";
char full[100] = "user ";
strcpy(full,strncat(name1,name2,
3));
name1[3] = '\0';
printf("%s", full);
return 0;
}
-
What is output after the execution of the snippet?
char name[50] = "first";
char fullname[100] = "full";
char nickname[50] = "nick";
printf("%s %s %s %s", name, nickname, fullname,
strcat(nickname, strcat(fullname, &name[0])));
Answer:
-
What is output after the execution of the snippet?
char name[50] = "first";
char fullname[100] = "full";
char nickname[50] = "nick";
printf("%s %s %s %s", name, nickname, fullname,
strcat(nickname, strcat(fullname, &name[0])));
Answer:first nickfullfirst fullfirst nickfullfirst
-
What is output after the execution of the snippet?
char tongue[] = "Peter Piper picked a peck of pickled
peppers.";
char twister[20];
strncpy(twister, tongue, 11);
tongue[0] = '\0';
printf("\n%s\n", tongue);
Answer:
-
What is output after the execution of the snippet?
char tongue[] = "Peter Piper picked a peck of pickled
peppers.";
char twister[20];
strncpy(twister, tongue, 11);
tongue[0] = '\0';
printf("\n%s\n", tongue);
Answer:Nothing will be displayed
-
What is output after the execution of the snippet?
char first[50] = "Fitzwilliam";
char last[50] = "Darcy";
char full[50];
printf("%s", strcat(strcpy(full, &first[4]), last));
Answer:
-
What is output after the execution of the snippet?
char first[50] = "Fitzwilliam";
char last[50] = "Darcy";
char full[50];
printf("%s", strcat(strcpy(full, &first[4]), last));
Answer:williamDarcy
-
What is output after the execution of the snippet?
char tmp1[15], tmp2[15], tmp3[15];
char str1[] = "Counter Terrorist";
char str2[] = "Los Angeles Lakers";
strcpy(tmp3, &str1[8]);
tmp3[9] = '\0';
strncpy(tmp2, &str2[0], 3);
tmp2[3] = '\0';
strncpy(tmp1, str1, 2);
tmp1[2] = '\0';
printf("%s %s %s",strcat(tmp2,"n"),tmp1,strcat(tmp3,"as"));
Answer:
-
What is output after the execution of the snippet?
char tmp1[15], tmp2[15], tmp3[15];
char str1[] = "Counter Terrorist";
char str2[] = "Los Angeles Lakers";
strcpy(tmp3, &str1[8]);
tmp3[9] = '\0';
strncpy(tmp2, &str2[0], 3);
tmp2[3] = '\0';
strncpy(tmp1, str1, 2);
tmp1[2] = '\0';
printf("%s %s %s",strcat(tmp2,"n"),tmp1,strcat(tmp3,"as"));
Answer:Losn Co Terroristas
-
Given the declaration below, how to you accomplish the instruction:
"Store in s1 the first three characters
of sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:
-
Given the declaration below, how to you accomplish the instruction:
"Store in s1 the first three characters
of sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:strncpy(s1, sss, 3); s1[3] = '\0';
-
Given the declaration below, how to you accomplish the instruction:
"Store in s2 the middle two-digit portion
of sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:
-
Given the declaration below, how to you accomplish the instruction:
"Store in s2 the middle two-digit portion
of sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:strncpy(s2, &sss[4], 2); s2[2] = '\0';
-
Given the declaration below, how to you accomplish the instruction:
"Store in s3 the final four digits of
sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:
-
Given the declaration below, how to you accomplish the instruction:
"Store in s3 the final four digits of
sss";
char sss[12] = "123-45-6789";
char s1[4], s2[3], s3[5];
Answer:strcpy(s3,&sss[7])s3[4] = '\0';
strncpy(s3, &sss[7], 4); s3[4] = '\0';
-
What does this
function myLength()
do?
Answer:
int myLength(char string[])
{
int index = 0, count = 0;
while(string[index] != '\0')
{
count++;
index++;
}
return count;
}
-
What does this
function myLength()
do?
Answer:
It counts the no. of chars in the string.
int myLength(char string[])
{
int index = 0, count = 0;
while(string[index] != '\0')
{
count++;
index++;
}
return count;
}
-
Which of the following gives the memory address of a variable a?
Choices:
a. a;
b. *a;
c. &a;
d. address(a);
-
Which of the following gives the memory address of a variable a?
Answer:
a. a;
b. *a;
c. &a;
d. address(a);
-
Given the code snippet below which among the following will give the memory address pointed to by pointer p? *
int *p, x;
p = &x;
Answer:
-
Given the code snippet below which among the following will give the memory address pointed to by pointer p? *
int *p, x;
p = &x;
Answer:
p
-
Which of the following gives the value stored at the address pointed to by pointer a?
Choices:
a. a;
b. val(a);
c. *a;
d. &a;
-
Which of the following gives the value stored at the address pointed to by pointer a?
Answer:
a. a;
b. val(a);
c. *a;
d. &a;
-
Whats the output after the execution of the program?
Answer:
#include
void somefun(int *i, int *j);
int main()
{
int i = 5, j = 2;
somefun(&i, &j);
printf("\n%d %d\n", i, j);
return 0;
}
void somefun(int *i, int *j)
{
*i = *i * *j;
*j = *j * *i;
}
-
Whats the output after the execution of the program?
Answer:
10 20
#include
void somefun(int *i, int *j);
int main()
{
int i = 5, j = 2;
somefun(&i, &j);
printf("\n%d %d\n", i, j);
return 0;
}
void somefun(int *i, int *j)
{
*i = *i * *j;
*j = *j * *i;
}
-
Whats the output after the execution of the program?
Answer:
#include
void somefun(int *i, int j);
int main()
{
int i=4, j=2;
somefun(&i, j);
printf("\n%d %d\n", i, j);
return 0;
}
void somefun(int *i, int j)
{
*i = *i * *i;
j = j * j;
}
-
Whats the output after the execution of the program?
Answer:
16 2
#include
void somefun(int *i, int j);
int main()
{
int i=4, j=2;
somefun(&i, j);
printf("\n%d %d\n", i, j);
return 0;
}
void somefun(int *i, int j)
{
*i = *i * *i;
j = j * j;
}
-
Whats the output after the execution of the program?
Answer:
#include
void MysteryFun(int q, int g);
int main()
{
int p=23, f=24;
MysteryFun(&p, &f);
printf ("\n%d %d", p, f);
return 0;
}
void MysteryFun(int q, int g)
{
q = q + g;
g = g + q;
}
-
Whats the output after the execution of the program?
Answer:
23 24
#include
void MysteryFun(int q, int g);
int main()
{
int p=23, f=24;
MysteryFun(&p, &f);
printf ("\n%d %d", p, f);
return 0;
}
void MysteryFun(int q, int g)
{
q = q + g;
g = g + q;
}
-
Whats the output after the execution of the program?
Answer:
#include
int main()
{
int a[] = {10, 20, 30, 40, 50};
int *j;
j = a;
j = j + 3;
printf("\n%d\n", *j);
return 0;
}
-
Whats the output after the execution of the program?
Answer:
40
#include
int main()
{
int a[] = {10, 20, 30, 40, 50};
int *j;
j = a;
j = j + 3;
printf("\n%d\n", *j);
return 0;
}
-
If a variable is a pointer to a structure, then which of the following operator is used to directly access data members of the structure through the pointer variable?
Choices:a. . operator
b. * operatorc. & operatord. -> operator
-
If a variable is a pointer to a structure, then which of the following operator is used to directly access data members of the structure through the pointer variable?
Answer:a. . operator
b. * operatorc. & operatord. -> operator
-
typedef struct {
char *name;
char gender;
} student_t;
void bar(student_t s[]) {
int check = strcmp(s[0].name,s[1].name);
switch(check) {
case 1: printf("%c", s[0].gender); break;
case -1: printf("%c", s[1].gender); break;
default: printf("Undefined");
}
}
int main() {
student_t studentA = {"Juan Dela Cruz", 'M'};
student_t studentB = {"June Doe", 'F'};
student_t students[] = {studentA, studentB};
bar(students);
return 0;
}
Answer:
-
typedef struct {
char *name;
char gender;
} student_t;
void bar(student_t s[]) {
int check = strcmp(s[0].name,s[1].name);
switch(check) {
case 1: printf("%c", s[0].gender); break;
case -1: printf("%c", s[1].gender); break;
default: printf("Undefined");
}
}
int main() {
student_t studentA = {"Juan Dela Cruz", 'M'};
student_t studentB = {"June Doe", 'F'};
student_t students[] = {studentA, studentB};
bar(students);
return 0;
}
Answer:F
-
typedef struct {
char *name;
char gender;
} student_t;
void bar(student_t s[]) {
int check = strncmp(s[0].name,s[1].name,2);
switch(check) {
case 1: printf("%c", s[0].gender); break;
case -1: printf("%c", s[1].gender); break;
default: printf("Undefined");
}
}
int main() {
student_t studentA = {"Juan Dela Cruz", 'M'};
student_t studentB = {"June Doe", 'F'};
student_t students[] = {studentA, studentB};
bar(students);
return 0;
}
Answer:
-
typedef struct {
char *name;
char gender;
} student_t;
void bar(student_t s[]) {
int check = strncmp(s[0].name,s[1].name,2);
switch(check) {
case 1: printf("%c", s[0].gender); break;
case -1: printf("%c", s[1].gender); break;
default: printf("Undefined");
}
}
int main() {
student_t studentA = {"Juan Dela Cruz", 'M'};
student_t studentB = {"June Doe", 'F'};
student_t students[] = {studentA, studentB};
bar(students);
return 0;
}
Answer:Undefined
-
For more questions about Structures and
Pointers to Structures, answer past exams that
I have provided for you. I cannot paste new
problems here in the reviewer because they take
up too much space. Hint: Start reviewing from
the most recent past exam up to the old. ;)
-
is near! LOL