Uploaded by kmgvv23

ㅈㅂ

advertisement
·
Quiz navigation
/ PROGRAMMING PRINCIPLES AND PRACTICE (CB1600702-004)
i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2021-12-16 16:31
Started on
Finished
State
2021-12-16 17:18
Completed on
47 mins 45 secs
Time taken
115.75 out of 140.00 (83%)
Grade
Information
Flag
question
The total exam time is 110 minutes and The total points of the nal exam is 270.
PLATO test (Short Answer Questions) - 140 points
28 Questions, 5 points for each question .
PLATO
28
5
.
Goorm EDU test (Programming Questions) - 130 points
6 Questions, 15~30 points for each question
Groom EDU
6
15~30
Show one page at a time
Finish review
[01] Line Counter (30 pts): Implement a program that counts the number of text lines in a text le. (
[02] Prime Checker (25 pts): Write a program to check whether a number is Prime or Not. (
)
[04] Cyclic Swap of N numbers (20 pts): Implement a program to swap numbers in cyclic order. (
Syllabus (kor)
)
[05] Student Records (15 pts): Implement a program that receives student information through standard input and then outputs it again. (
)
Syllabus (eng)
Grade/Attendance 
[06] Encryption and Decryption (15 pts): Implement a program to decode an encoded string. (
Progress status
Grades
Question 1
Sending Message
Mark 5.00 out of
5.00
Others 
)
[03] Palindrome (25 pts): Write a program to determine whether a word is a palindrome or not. (Palindrome: A word, phrase, or sequence that reads the
same backward as forward, e.g., madam or racecar.) (
)
Course Info 
Students Noti cations 
.
To help you in managing your exam time, the followings are some previews of Goorm EDU programming problems.
Course Home
Smart-Attendance
/ [Final:140]
Correct
What will be the output of the following program ?
Flag
question
Activities/Resources
-32
nC:
 , celsius: 10
)
Execution Result
 .00
#include <stdio.h>
int main(void)
{
int nF = 50, nC = 0;
float fahrenheit = 50.0;
float celsius = 0.0;
nC = 5/9 * nF – 32;
celsius = (fahrenheit – 32) * 5 / 9;
printf("nC: %d, ", nC);
printf("celsius: %.2f\n", celsius);
return 0;
}
Question 2
Partially correct
What will be the output of the following program ?
Mark 2.50 out of
5.00
Flag
question
2
Execution Result

4

#include <stdio.h>
int main(void)
{
int a=2, b=1, c=3;
if (a<b<c)
printf("1\n");
else
printf("2\n");
if (a<b && b<c)
printf("3\n");
else
printf("4\n");
return 1;
}
Question 3
Correct
What will be the output of the following program ?
Mark 5.00 out of
5.00
Flag
question
850
Execution Result

#include <stdio.h>
int main(void)
{
int i, j, num=0;
for (i=0 ; i<100 ; i+=2)
for (j=0 ; j<50 ; j+=3)
num++;
printf("%d\n", num);
return 0;
}
Question 4
Partially correct
What will be the output of the following program ? The source code is compiled and executed in a 32 bit machine.
Execution Result
Mark 4.00 out of
5.00
Flag
question
4
sizeof(a) :

12
sizeof(b) :

9
sizeof(c) :

4
sizeof(d) :
sizeof(*d) :

1

#include <stdio.h>
int main(void)
{
unsigned int a = 1;
int b[3] = {0};
char c[] = "Good Luck";
char * d = c;
printf("sizeof(a) : %d\n", sizeof(a));
printf("sizeof(b) : %d\n", sizeof(b));
printf("sizeof(c) : %d\n", sizeof(c));
printf("sizeof(d) : %d\n", sizeof(d));
printf("sizeof(*d) : %d\n", sizeof(*d));
return 1;
}
Question 5
Correct
What will be the output of the following program?
Mark 5.00 out of
5.00
Flag
question
246
Execution Result

#include <stdio.h>
int main() {
int a[3][3] = {1, 2, 3, 4, 5, 9, 8, 7, 6};
printf("%d", a[0][1]);
printf("%d", a[1][0]);
printf("%d", a[2][2]);
return 0;
}
Question 6
Correct
Mark 5.00 out of
5.00
Fill in or choose the proper words for the blanks.
In terms of Scope, variables can be classi ed into two types.
In the case of a variable declared in a function area, it is valid within the function and is called a
Flag
question
local
If a variable is declared outside function area, it is valid in all functions of the source le and is called a
 variable.
 variable.
global
In terms of Lifetime, memory allocated for a variable/object can be classi ed into three types.
 : Variables that are created when the program is started (allocated memory) and remain until the end of the program; all global
Static
variables belong to this type. Its lifetime is the entire duration of the program's execution. A variable of this type is stored in the

Data Segment
 : Variables that are created when a function is called, allocated memory, and disappears when the function exits. If there are no
Automatic
additional directives, local variables belong to this type. Its lifetime begins when program execution enters the function or statement block or
compound and ends when execution leaves the block. A variable of this type is stored in the
Stack

 : Its lifetime begins begins when memory is allocated for the object (e.g., by a call to malloc()) and ends when memory is
Dynamic
deallocated (e.g., by a call to free()). It is stored in the
Heap

Function Call Mechanisms in the C language
In the
 mechanism, it is impossible to change the contents of a variable used in a function call or to implement a
call by value
function that returns more than one result. Otherwise, in the
parameter.
call by address value
Command Line Arguments
When command line arguments are passed to main() function,
indicates the array of pointers to command line arguments.
 mechanism, those are possible by pass the pointer as a
 denotes the number of command line arguments and argv
argc

File Types/Modes : Generally speaking, a le can be classi ed into two types or modes. Fill in the blanks [1], [2] in the following gure with the
appropriate words.
[1]
Question 7
Correct
Mark 5.00 out of
5.00
Flag
question
 , [2] binary
text

We want to write the program on the program 1 that outputs the operation result for the values stored in two integers a and b as a function. Complete
the program 2 to show the same result as the program 1.
(DO NOT include blanks in your code unless necessary for automatic scoring)
Program 1
Program 2
#include <stdio.h>
int main(void)
{
int a = 1, b =2, sum;
sum = a + b;
printf("sum = %d\n",sum);
sum = a - b;
printf("sum = %d\n",sum);
sum = a * b;
printf("sum = %d\n",sum);
#include <stdio.h>
void sum(int n);

int main(void)
{
int a = 1, b =2;
sum(a+b);
sum(a-b);
sum(a*b);
return 0;
}
return 0;
}
void sum(int n)
{
printf("sum = %d\n",n);

}
Question 8
Correct
What will be the output of the following program ?
Mark 5.00 out of
5.00
Flag
question
15
v1:
 , v2: 5
 , v3: 15
Execution Result

#include <stdio.h>
int var=0;
int func1() {
int i=0;
while (i++ < 5) var++;
return var;
}
int func2() {
int i=0;
int var=0;
while (i++ < 5) var++;
return var;
}
int func3() {
int i=0;
static int var = 0;
while (i++ < 5) var++;
return var;
}
int main()
{
int i=0, v1, v2, v3;
while (i++ < 3) {
v1 = func1();
v2 = func2();
v3 = func3();
}
printf("v1: %d, v2: %d, v3: %d\n", v1, v2, v3);
return 0;
}
Question 9
Correct
What will be the output of the following program?
Mark 5.00 out of
5.00
Flag
question
 , 3
5
Execution Result

#include <stdio.h>
void increment(int *v) {
(*v)++;
}
void func1(int *a, int b) {
increment(a);
increment(&b);
}
int main(void){
int x = 2, y = 3, k;
for (k = 0; k < 3; k++)
func1(&x, y);
printf("%d, %d\n", x, y);
return 0;
}
Question 10
Correct
What will be the output of the following program ?
Mark 5.00 out of
5.00
Flag
question
11
 , 17
Execution Result
 , 23
 , 27
 , 37

#include <stdio.h>
int main(void)
{
int na[] = {37, 23, 11, 27, 17};
int nlen = sizeof(na)/sizeof(int);
int i,j,t;
for (i=nlen-1; i > 0; i--) {
for (j=0; j<i; j++)
if (na[j] > na[j+1]) {
t=na[j];
na[j] = na[j+1];
na[j+1] = t;
}
}
printf("%d, %d, %d, %d, %d\n",
na[0], na[1], na[2], na[3], na[4]);
return 0;
}
Question 11
Correct
Mark 5.00 out of
5.00
The following source code traverses the string to get the Comma Separated Integer Values and prints out the sum of them as shown in the execution
result below. Fill the blank to complete the code.
Execution Result
Flag
question
Sum of all values in CSV[123,456,789] : 1368
DO NOT INCLUDE INESSENTIAL WHITE SPACES FOR AUTOMATIC SCORING(
)
#include <stdio.h>
int main(void)
{
int i;
int num=0, sum=0;
char str[]="123,456,789";
for(i=0;str[i];i++) {
if (str[i] == ',') {
sum += num;
num = 0;
} else {
(str[i]-'0')
num = num*10 +
 ;
}
}
sum += num;
printf("Sum of all values in ");
printf(" CSV[%s] : %d", str,sum);
return 0;
}
Question 12
Correct
Mark 5.00 out of
5.00
The following source code traverses the string to get the Comma Separated Integer Values and prints out the sum of them as shown in the execution
result below. Fill the blank to complete the code.
Execution Result
Flag
question
Sum of all values in CSV[123,456,789] : 1368
DO NOT INCLUDE INESSENTIAL WHITE SPACES FOR AUTOMATIC SCORING(
)
#include <stdio.h>
int main(void)
{
char *pstr;
int num=0, sum=0;
char str[]="123,456,789";
for(
pstr=str
 ; *pstr; pstr++) {
if (*pstr == ',') {
sum += num;
num = 0;
} else {
num = num*10 + (*pstr -'0');
}
}
sum += num;
printf("Sum of all values in ");
printf(" CSV[%s] : %d", str,sum);
return 0;
}
Question 13
Incorrect
Mark 0.00 out of
5.00
Flag
question
mystrcpy() and mystrcpy2() are functions which perform string copy.
Compare mystrcpy() with mystrcpy2().
Choose a proper one for the blank [1] to make mystrcpy2() work correct
1.
2.
3.
4.
(*dest)++ = *src++;
*dest++ = *src++;
*dest++ = (*src)++;
(*dest)++ = (*src)++;
4

void mystrcpy(char dest[], char src[]) {
int i=0, j=0;
while (src[i])
dest[j++] = src[i++];
dest[j] = '\0';
}
void mystrcpy2(char *dest, char *src) {
while (*src)
[ 1 ]
*dest='\0';
}
Question 14
Correct
What will be the output of the following program ?
Mark 5.00 out of
5.00
Flag
question
(a, b)-(3,7), (a,b)-( 3
 , 7
Execution Result
 ), (a,b)-( 3
 , 7
 ), (a,b)-( 7
 , 3
 )
#include <stdio.h>
void swap1(int a, int b)
{
int t = a;
a = b;
b = t;
}
void swap2(int *a, int *b)
{
int *t = a;
a = b;
b = t;
}
void swap3(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int main(void)
{
int a=3, b=7, t;
printf("(a,b)–(%d,%d), ",a,b);
swap1(a,b);
printf("(a,b)–(%d,%d), ",a,b);
swap2(a,b);
printf("(a,b)–(%d,%d), ",a,b);
swap3(a,b);
printf("(a,b)–(%d,%d)",a,b);
return 0;
}
Question 15
Correct
What will be the output of the following program?
Mark 5.00 out of
5.00
Flag
question
BUTTER
Execution Result

#include <stdio.h>
int main(void) {
char str[] = "RASEIJTLKTWEUASB";
char *p = NULL;
int len = sizeof(str) - 2;
for (p = str + len; p >= str; p -= 3) {
printf("%c", *p);
}
printf("\n");
return 0;
}
Question 16
Correct
What will be the output of the following program?
Mark 5.00 out of
5.00
Flag
question
2
Execution Result

3

0

#include <stdio.h>
int main(void)
{
int a[10] = {1, 2, 3, 4, 5, 6};
int *p = a;
int *q = a + 3;
printf("%d\n", *(a+1));
printf("%d\n", *(p+2));
printf("%d\n", *(q+3));
return 0;
}
Question 17
We write a program calculating the bonacci sequence value using a recursive function.
Mark 1.50 out of
5.00
f ibo(n) = f ibo(n − 1) + f ibo(n − 2), where
n ≥ 2, f ibo(1) = 1, f ibo(0) = 0
Partially correct
Flag
question
Write the execution results of the program for the various input values for "n".
input (n)
Result of printf() - 1
1
fibo(1) = 1
3
fibo(3) =
5
fibo(5) = 5
10
fibo(10) =
Result of printf() - 2
# of fibo() called : 1
2

34
# of fibo() called : 5

# of fibo() called :
15
# of fibo() called :
176


#include <stdio.h>
int g_ncalled = 0;
int fibo(int n) {
g_ncalled++;
if (n==0) return 0;
if (n==1) return 1;
return fibo(n-1) + fibo(n-2);
}
int main(void) {
int n;
scanf("%d", &n);
printf("fibo(%d) = %d\n", n, fibo(n));
// printf() - 1
printf("# of fibo() called : %d\n", g_ncalled); // printf() - 2
return 0;
}
Question 18
Correct
What will be the output of the following program?
Mark 5.00 out of
5.00
Flag
question
result: 25
Execution Result

#include <stdio.h>
int foo(int a) {
static int count = 0;
count++;
return goo(a, count);
}
int goo(int a, int count) {
static int sum = 5;
if(count < 3){
sum += a;
count++;
return foo(10);
} else {
return sum;
}
}
int main() {
int res = foo(10);
printf("result:%d", res);
return 0;
}
Question 19
Correct
Mark 5.00 out of
5.00
Flag
question
3. Which of the following statements about the structure variable d is incorrect?
1
2
3
4
Mark 1.00 out of 1.00
The correct answer is: 1
1
2
struct user {
int number;
char *name;
};
user d;
Question 20
Correct
Mark 5.00 out of
5.00
Flag
question
typedef struct {
int number;
char *name;
} user;
user d;
For each line from 가 to
typedef struct student {
int id;
char *pname;
double points;
} STUD;
int main(void) {
STUD s1 = {1, "Choi", 9.9};
STUD *ps1 = &s1;
// ①
ps1->id = 3; // ②
(*ps1).id = 4; // ③
*ps1.id = 5; // ④
OK
OK
OK
typedef struct {
int number;
char *name;
} d;
in the source code below, determine whether it raises an error or not.
#include <stdio.h>
s1.id = 2;
3
Error




4
struct user {
int number;
char *name;
} d;
return 0;
}
Question 21
Correct
Complete the source code below by lling in each blank with a proper code to achieve the following execution result.
Execution Result
Mark 5.00 out of
5.00
23, A, 97.2
Flag
question
#include <stdio.h>
int main(void)
{
struct stud {
int id;
char
grade;
double points;
} s1, *ps1 = &s1;
s1 .id
 = 23;
s1 .grade
 = 'A';
s1 .points
 = 97.2;
printf("%d, %c, %.1lf\n",
ps1 ->id
 ,
ps1 ->grade
 ,
ps1 ->points
 );
return 0;
}
Question 22
Partially correct
For each code line 1) ~ 4) in the following source code, determine whether it raise a syntax error.
// 1)
Mark 3.75 out of
5.00
OK
Flag
question
Error
Mark 1.00 out of 1.00
s2 = s1;
The correct answer is: OK
// 2)
OK
Error
Mark 0.00 out of 1.00
s3 = {2, Park, 0.1};
The correct answer is: Error
// 3)
OK
Error
Mark 1.00 out of 1.00
aN2 = aN1;
The correct answer is: Error
// 4)
OK
Error
Mark 1.00 out of 1.00
aMsg2 = "World";
The correct answer is: Error
#include <stdio.h>
typedef struct student {
int id;
char *pname;
double points;
} STUD;
int main(void)
{
STUD s1 = {1, "Choi", 9.9};
STUD s2;
struct student s3;
char aMsg1[] = "Hello";
char aMsg2[10];
int aN1[]={1, 2, 3};
int aN2[3];
s2 = s1;
// 1)
s3 = {2, Park, 0.1};
// 2)
aN2 = aN1;
// 3)
aMsg2 = "World";
// 4)
return 0;
}
Question 23
Correct
We want to get the following execution result from the program below.
Execution Result
Mark 5.00 out of
5.00
Flag
question
ID: 11
NAME: a
GRADE: 3.140000
Select one that is not proper for the the blank [1] from the followings.
1.
2.
3.
4.
3

p->
(*p).
*p.
p[0].
#include <stdio.h>
struct stud {
int id;
char name;
double grade;
};
void print(struct stud* p)
{
printf("ID: %d\n", [1]id);
printf("NAME: %c\n", [1]name);
printf("GRADE: %f\n", [1]grade);
}
int main(void)
{
struct stud s = {11, 'a', 3.14};
print( &s );
return 0;
}
Question 24
Correct
What will be the output of the following program ? Assume that the program is compiled and executed in a 32 bit environment.
Execution Result
Mark 5.00 out of
5.00
Flag
question
8
sizeof(STUD) :
sizeof(pnuecs):

24

ps : 0x0062FE7C
ps+1:
0x0062FE84
ps2->pname :

0x0062FE90

#include <stdio.h>
typedef struct student {
int id;
char *pname;
} STUD;
int main(void)
{
STUD pnuecs[] = {{1, "Choi"}, {2, "Park"}, {3, "Kim"}};
STUD* ps = pnuecs;
STUD* ps1 = &pnuecs[0];
STUD* ps2 = &pnuecs[2];
printf("sizeof(STUD) : %d\n", sizeof(STUD));
printf("sizeof(pnuecs): %d\n\n", sizeof(pnuecs));
printf("ps : 0x%p\n",ps);
printf("ps+1: 0x%p\n",ps+1);
printf("ps2->pname : 0x%p\n",&(ps2->pname));
return 0;
}
Question 25
Correct
Mark 5.00 out of
5.00
Flag
question
The following 2 source codes show 2 general methods for struct array traversal. That is, the left source code shows the index based struct array
traversal and the right source code shows the pointer based struct array traversal. The execution results of the 2 codes should be the same.
Fill in the blanks of the right source code to complete the pointer based struct array traversal.
[1]
ps=pnuecs
[2]
ps->id
[3]
ps++



#include <stdio.h>
typedef struct student {
int id;
char *pname;
double points;
} STUD;
#include <stdio.h>
typedef struct student {
int id;
char *pname;
double points;
} STUD;
int main(void) {
int i;
// index
STUD pnuecs[] = { {1, "Choi", 9.9},
{2, "Park", 0.1},
{3, "Kim", 5.0 },
{4, "Lee", 3.0 },
{5, "Moon", 9.5 },
{6, "Kang", 7.0 },
{7, "Jeon", 0.9 },
{-1, NULL, 0 } };
int main(void) {
STUD *ps;
// pointer
STUD pnuecs[] = { {1, "Choi", 9.9},
{2, "Park", 0.1},
{3, "Kim", 5.0 },
{4, "Lee", 3.0 },
{5, "Moon", 9.5 },
{6, "Kang", 7.0 },
{7, "Jeon", 0.9 },
{-1, NULL, 0 } };
for (i=0; pnuecs[i].id >= 0; i++ ){
printf("[%d:%s] = %lf\n",
pnuecs[i].id, pnuecs[i].pname,
pnuecs[i].points);
}
for([1]; [2] >= 0; [3]) {
printf("[%d:%s] = %lf\n",
ps->id, ps->pname,
ps->points);
}
return 0;
return 0;
}
Question 26
Partially correct
Mark 4.00 out of
5.00
}
We would like to classify the lifetime of the following variables (or memory block).
For each of the following classi cations, determine whether it is correct or not.
1. g_sum at line 5 - static
Flag
question
2. num at line 7 - dynamic
X
X
3. num at line 12 - automatic
4. ns at line 19 dynamic
X


O


5. pns_copy at line 20 - dynamic
1
2
3
4
5*
6
7*
8
9
10
11
12*
13
14
15
16
17
18
19*
20*
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Question 27
Incorrect
Mark 0.00 out of
5.00
Flag
question
X

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int g_sum = 0;
int add(int num) {
g_sum += num;
return g_sum;
}
int add2(int num) {
static int sum = 0;
sum += num;
return sum;
}
int main(void) {
int ns[] = {1, 2, 3, 4, 5};
int *pns_copy = malloc(sizeof(ns));
int i, sum;
assert(pns_copy);
for (i=0; i<5; i++) {
pns_copy[i] = ns[i];
sum = add(ns[i]);
}
printf("sum of ns : %d\n", sum);
for (i=0; i<5; i++) {
sum = add2(pns_copy[i]);
}
printf("sum of pns_copy : %d\n", sum);
free(pns_copy);
return 0;
}
By executing the following program, we got the following execution result. Besides, we successfully create 2 new les, "values.txt" and "values.bin".
Write the size of "values.txt" and "values.bin". (
)
size of "values.txt" :
size of "values.bin" :
50
20
.
"values.txt"
"values.bin"
.
 bytes
 bytes
Execution Result
sizeof(float) = 4
1.20,2.30,3.40,4.50,5.60,1000.20,2000.30,3000.40,4000.50,5000.60,
#include <stdio.h>
int main(void)
{
FILE *fptxt, *fpbin;
float vals[10] = {1.2, 2.3, 3.4, 4.5, 5.6,
1000.2, 2000.3, 3000.4,
4000.5, 5000.6};
int i;
fptxt = fopen("values.txt","w");
fpbin = fopen("values.bin","wb");
printf("sizeof(float) = %d\n", sizeof(float));
for (i=0; i<10; i++) {
printf("%.2f,",vals[i]);
fprintf(fptxt,"%.2f,",vals[i]);
}
fclose(fptxt);
fwrite(vals, sizeof(float), 10, fpbin);
fclose(fpbin);
return 0;
}
Question 28
Incorrect
Mark 0.00 out of
5.00
Flag
question
Your program is "prob.exe" and its implementation code is shown in the below.
What will be the output of the program if you run the problem with the following command ?
prob 50 hello world!
Command & Execution Result
prob 50 hello world!
5
50


#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
printf("Argument 2: %s\n", argv[2]);
return 0;
}
Finish review
Download