Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

VIETNAMESE - GERMAN UNIVERSITY

Dr. Hai Van Nguyen

INTRODUCTION TO PROGRAMMING
FINAL EXAMINATION

August, 2023
Duration: 90 minutes

Full name:....................................................
Student ID:....................................................
INSTRUCTIONS

1. Answer ALL the questions.

2. Correcting fluid is not permitted. Do not use red ink.

3. Documents are NOT allowed.

4. All calculators are permitted. All other handheld devices are NOT allowed.

5. Students should show all working in their answers.

6. Write your solutions on exam papers.

Question 1 2 3 4 5 sum
Max. points 24 21 30 11 14 100
Obtained points
Question 1 (24 marks)
Identify and correct the errors in each of the following:

a) while ( c =< 5 )
{
product *= c
c++;
}

b) if ( number >= largest );


largest == number;

c) print( ”The product is &d \n,” x * y );

d) int b[10] = { 0 }, i;
for (i = 0; i <= 10; i++)
{
b[i] = 1;
}

e) int number;
printf(”%d \n”, number);

f) float x = 19.34;
float xPtr = &x;
printf(”%f\n”, xPtr);
Student ID:

Introduction to Programming - Final examination

Question 2 (21 marks)


Show the output produced by each of the following program fragments. Assume that i, j
and k are int variables.
a)

i) i=5, j=3;
printf(”%d %d”, i/j, i%j);

ii) i=2, j=3;


printf(”%d ”, (i+10)%j);

iii) i=7, j=8, k=9;


printf(”%d ”, (i+10)%k/j);

Answer:

b)

i=1;
while( i <= 28)
{
printf(”%d”,i);
i *=2;
}

Answer:

2
Student ID:

Introduction to Programming - Final examination

c)

i=938;
do
{
printf(”%d”,i);
i/=10;
}while(i>0);

Answer:

d)

for( i=5,j=i-1;i>0,j>0;i–,j=i-1)
printf(”%d”,i);

Answer:

3
Student ID:

Introduction to Programming - Final examination

Question 3 (total 30 marks)


Please try to write missing statements.
a) Sum of natural numbers using recursion

#include<stdio.h>
int sum(int n);
int main()
{
int number, result;
printf(”Enter a positive integer: ”);
.............................................
.............................................
printf(”sum = %d”, result);
return 0;
}
int sum(int n)
{
if (n ! = 0)
// sum() function calls itself
.............................................
else
.............................................
}

4
Student ID:

Introduction to Programming - Final examination

b) Program to calculate the sum of array elements by passing to a function

#include<stdio.h>
float calculateSum(float num[]);
int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
// num array is passed to calculateSum()
.............................................
printf(”Result = %.2f”, result);
return 0;
}
float calculateSum(float num[])
{
.............................................
.............................................
{
.............................................
}
.............................................
}

5
Student ID:

Introduction to Programming - Final examination

c)

#include<stdio.h>
int main()
{
char name[50];
int marks, i, num;
printf(”Enter number of students: ”);
scanf(”%d”, &num);
.............................................
fptr = (fopen(”C:\ \student.txt”, ”a”));
.............................................
{
printf(”Error!”);
exit(1);
}
for(i = 0; i < num; i++)
{
printf(”For student %d \n Enter name: ”, i+1);
.............................................
printf(”Enter marks: ”);
.............................................
fprintf(fptr,”\n Name: %s \nMarks=%d\n”, name, marks);
}
.............................................
return 0;
}

6
Question 4 (total 11 marks)
Write a function to find the greatest common factor of two given numbers using Recur-
sion.
Student ID:

Introduction to Programming - Final examination

Question 5 (total 14 marks)


What output do the following program fragments produce?
a)

#include<stdio.h>
int main()
{
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2];
printf(”*ptr = %d \n”, *ptr);
printf(”(ptr+1) = %d \n”, *(ptr+1));
printf(”(ptr-1) = %d”, *(ptr-1));
return 0;
}

Answer:

8
Student ID:

Introduction to Programming - Final examination

b)

#include<stdio.h>
#include <string.h>

// create struct with person1 variable


struct Person
{
char name[50];
int citNo;
float salary;
} person1;
int main()
{
// assign value to name of person1
strcpy(person1.name, ”VGU Students”);
// assign values to other person1 variables
person1.citNo = 2006;
person1.salary = 1500;
// print struct variables
printf(”Name: %s\n”, person1.name);
printf(”Citizenship No.: %d\n”, person1.citNo);
printf(”Salary: %.2f”, person1.salary);
return 0;
}

Answer:

9
BLANK PAGE FOR EXTRA WORKING (Any work here will NOT be marked.)

You might also like