You are on page 1of 44
\ THE UNIVERSITY OF DODOMA COLLEGE OF LNFORMATICS AND VIRTUAL EDUCATION SCHOOL OF INFORMATICS SITY 0, cing Ko UNDERGRADUATE UNIVERSITY EXAMINATIONS FIRST SEMESTER 2014/2015 CS 110: INTRODUCTION TO HIGH LEVEL PROGRAMMING LANGUAGE Date: 17" February 2015 Time Allocated: 3 Hours Instructions: 4 1. This question paper consists of section A and section B. 2. Answer all question from section A and any two questions Jrom section B 3. All University of Dodoma examination regulations apply Page | of 5 SECTION A: (50 Marks) Question One (a) What do you understand by computer programming? 11 Mark (b) With an example, give your definition of variables in programming {1 Mark} (©) With an example show the relationship between variables and RAM {Mark} (@) Write a short progrem of you own choice, to demonstrate SEQUENCE in programming [2 Marks} (©) Using MTL (RAM diagrams) show the behavior of variables in the program that you have written in 1(d) (2 Marks} () Modify your program in 1 (d)_ to _ include SELECTION/BRANCHING (2 Marks} (e) Using MTL (RAM diagrams) show the behavior of variables in the program that you have written in 1(f) [2 Marks} (h) Modify your program in 1 (f) to include REPETITION/LOOPING [3 Marks} (i) Using MTL (RAM diagrams) show the behavior of variables in the program that you have written in 1(h) 13 Marks + (j) Modify your program so that it can be organized in at least 5 functions (i.e, menu(), input(), process(), output() and main() (7 Marks} Page 2 of 5 return n+ xPunetion(n - 1); (a) 12 3) Analyze the following code: #include include void xFunction(int x{], int length) t cout <<" << x[length - 1]; ~> xFunction(x, length - 1); } int main() int x{] = {1, 2, 3, 4, 5}; » xFunction(x, 5); getchQ); (a) The program displays 123 4 6. (b) The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. (c) The program displays 5 4 3 2 1. (Wythe program displays 5 4 3 2 | and then raises an ArrayIndexOutOfBoundsException. 4) Fill in the code to complete the following function for binary search. Page 3 of 19 int binarySearch(const int list{], int ke { if (low > high) // The list has been exhausted without a match return -low ~ 1; // Return -insertion point - | Y, int low, int high) int mid = (low + high) / 2; if (key 1) a << (length - 1)<<""; Page 4 of 19 for(k=pt+1;k<=n;k++) if(min>a[k]) { min=a[k]; loc=k; } } temp=a[p]; a[p]=a[loc]; a{loc]=temp; Note: Use Graphical presentation not coding. SECTION B (40 Marks) ~ Question Seven [20 Marks} (a) Construct the heap and perform the delete - min operation for the following inputs: 142, 543, 123, 65, 453, 879, and 572. Draw neat diagrams. (b) Draw the binary search trees that will be constructed for the insertion of 8 names as: Tim, Dot, Era, Roy, Tom, Kim, Guy, Any. (c) If the characters 'D', 'C’, 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? (d) When inorder traversing a tree resulted EAC K F HD BG; what the preorder traversal would return? i ‘ pies) Question Eight [20 Marks] (a) What are the infix, postfix and prefix form of the expression 14/(3-2*(4+3)-(8/2)), Page 18 of 19 CS 110: INTRODUCTION TO HIG PROGRAMMING LANGUAGE, TIME: 2 HOURS INSTRUCTION: Answer all questions: Question T (a) What is the output of the following program? 1 Assume geeks() is called in function main, #include void geeks() ‘ Consider the address of a variable var is Ox12344 int var = 20; int *ptr, pir= &var, cout << “Phase I" << “\n"; cout << "Value at ptr=" << ptr << "\n" ‘cout << "Value at var =" << var <<" cout << "Value at *ptr'=" << *ptr << *pir= 30 ‘cout << "Phase II” << “tn”; ‘cout << "Value at ptr=" << ptr<<"\n'; ‘cout << "Value at var =" << var <<" cout <<"Value at *ptr'=" << *ptr << ) Z| ii Consider this code segment int list[10}; for (int i= 0,1<5; i++) { | listfi] =i i 5, if('%3—=0) Aisi} = Hist) +i, else Aist{i) = list{i) void main() intx, y, float hisa; x=2; y=3; hhisa = Guy) sum=x+y; cout<<"The sum is: ” < Ainclude void gecks() ( “Consider the address of a variable var is 0x12344 int var = 20; int *ptr, pir= &var, cout << “Phase I" << “\n"; cout << "Value at ptr=" << ptr << "\ cout << "Value at var =" << var <<"\n", cout << "Value at *ptr=" << *pir << pur =3 cout << "Value at ptr cout << "Value at var cout << "Value at *pt i, Cor int list{10}; for (int i= 0, 1< 5; i++) der this code segment list{i) = list{i) - , Couk & Lst RODUCTION TO HIGH LEVEL PROGRAMMING LANGUAGE (2) What is the output of the following program? Assume geeks() is called in function mai (b) What is the error of the following program? 1 Debug the following code #ineludesiostream> void main() ( int, y; float hisa; x=2; y hisa = Gvy) sum=x+y; ‘cout<<"The sum is: " < finclude void main() { int m, n; char ch; string s, n=7, m=6, chea, switeh(ch) { case a s=ntm, ‘cout<<"The sum is: " < | | void gecks() | ‘ i {uConsider the address of a variable var is Ox12344 | int var = | imp pir= &var, cout <<“Phase I" <<“ cout << "Value at pir=" << ptr << "nt, 07 | cout <<"Value at var =" << var <<"\n'; “* cout <<"Value at *pir =" << tptr <<" | *pir=30 | cout << “Phase II” <<"“\n"; cout << "Value at pir=" << pir <<"\n",™?, tou ceVahe atin" evar cers re ainem cain meet peepee } \ i Consider this code segment | int fist10} ' for (inti = 0, 1<5;i+#) { ist) =i 1-5; ifi%3 0) list( = sti) + i; | else | Hist) = list) =i ) Introduction to Programming TEST 1 (45 Min) Question 1 Using RAM diagrams dissect the program below int ongeza(int y, int y) rotumn(xty) int ziishacint x, int y) ‘ roturn(x*y), it gawa(int 6, iat y) rotumn(x/y)s m0) inta,b; cin>>b; ccout<<"Jumla ni #finclude int main() int a; int b; float wastani; cout<<"ingiza kiasi cha ng'ombe"<>a; cout<<"ingiza lita za maziwa"<> wastani = b/a; cout< | | #include int main() \< | float value; //pointer to value float *pov; cout<<"ingiza nambari yenye decimali" <>value; pov = &value; cout<>x; cin>>y; ZXtY; cout< Which of the followings identifiers are illegal and why? [4 Marks] a, my-Age . continue. . xyz123 Bill ThislsALongOne so is this one amount 2ndclass 5. Write the missing statements in the following program so that it prompts the user to input two numbers. If one of the numbers is 0, the b, ©. d e. re & h Page 2 of 13 program should output a message indicating that both numbers must be nonzero. If the first number is greater than the second number, it outputs the first number divided by the second number; if the first number is less than the second number, it outputs the second number divided by the first number; otherwise, it outputs the product of the numbers [4 Marks] #include void main() { double firstNum, secondNum; cout<< "Enter two nonzero numbers: my cin>>firstNum>>secondNum; cout<=y) sum += x; cout << "x is bigger" << endl; else sum += y; cout << "y is bigger" << endl; a. Is the statement syntactically incorrect? Why? {1 Mark} b. How could it be changed to be syntactically correct? {1 Mark] 2. Suppose that x, y, and z are int variables, and x = 10, y = 15, and z= 20. Determine whether the following expressions evaluate to true or false. [1 Mark Each] Page 3 of 13 a, (x <= 5) || < 15) b. (x != 5) && (y !=z) ©. (X<+y- 2) && (y >= z) || Z-2 != 20) 92. Rewrite the following as a for loop. [3 Mark, int i= 0, value = 0; — while (i <= 20) if (i % 2 = 0 && i <= 10) value = value +i * i; else if (i % 2 == 0 && i> 10) value = value + i; else value = value - i; i+]; } cout << "value =" << value << endl; 4 4. Write C++ statements to do the following: [1 Mark Each} a. Declare an array alpha of 15 components of type int. b. Set the value of the fifth component of the array alpha to 35. ©. Set the value of the fourth component of the array alpha to three times the value of the eighth component minus 57. 5 ff. Consider the following function prototype: void testDefaultParam(int a, int b = 7, char z="); Which of the following function calls is not correct? Why? [4 Marks] . testDefaultParam(5); . testDefaultParam( ); . testDefaultParam(5, 8); |. testDefaultParam(6, '#'); aegce Page 4 of 13 e. testDefaultParam(0, 0, '*"); Question Three: [20 Marks} What is the output of the following programs/ code fragments? [3 Marks each] 1. #include #include void main() { inti, j; for(i=1; i<=5; i++) { forG=1; j<=i; j++) cout<<"*"; cout< #include void main() int list{10]={1,2,3,4,5,6,7,8,9,10}; int i; for(i=0;i<5;i++) Page 5 of 13 int temp=list[i); list{iJ=list{9-i); list[9-i]-temp; for(i=0;i<10;i++) cout< #include void main() { . int *p; int x = 37; cout << "x =" << x << endl; p= &x; cout << "*p = "<< *p<<", x ="< #include void funOne(int a, int& b, char v); void funTwo(int& x, int y, charé& w); void main() int num1, num2; char ch; num| = 10; num2 ch= cout << "Inside main: num] =" < void main () int a, b; bool found; cout << "Enter two integers: " ; cin >> a>>b; ifa>a*b&& 10b; else { found =2* a const int N = 2,137; void main () { int a, b,c, a=3; b=; c=ctd; N=at+c; For (i= 3; i <=N; i++) { 4 cout void main() Page 9 of 13 { int SomeVariable = 5; cout << "SomeVariable: " << SomeVariable << "\n"; int *pVar = & SomeVariable; pVar= 9; cout << "SomeVariable: "<< *pVar <<"\n"; } 5. # include # include void main( ) { float num; cin>>num; switch (num) { case 1.5: cout << "The number is 1.5" << endl; break; case 2.5: cout << "The number is 2" << endl; break; default: cout << "The number is neither 1.5 nor 2.5" << endl; break; } getch(); Page 10 of 13, SECTION B (40 Marks) Question Five 1. Write a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed. For example, the value of _reverseDigit(12345) is 54321; the value. of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235. [6 Marks] R Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order. [6 Marks] Percentage marks attained by a student in three exams are to be entered to a computer. An indication of Pass or Fail is given out after the three marks are entered. The criteria for passing are as follows: A student passes if all three examinations are passed. Additionally a student may pass if only one subject is failed and the overall average is greater than or equal to 50. The pass mark for an individual subject is 40. Write a C++ program to implement this task. [8 Marks] Question Six: 1. Write a program which will accept the first letter of a college of UDOM and print out the full name of that college. For example if a user enters E the program will print Earth Science. Assume that the Colleges at UDOM are Earth Science (E), Informatics and Virtual Education (1), Natural and Mathematical science (N), Hamanities and Social Science (S), Business and Law (B) and Health and Allied Science (H). [6 Marks} 2. Write a C++ program to implement the following requirement: Poge 11 of 13 A set of examination marks expressed as percentages are to be processed by computer, Data entry is terminated by entering a negative percentage value. As each examination mark is entered jt should be validated as being either a valid percentage or as being a negative number. The program should ask for the data entry to be repeated until a valid value is entered. The program should then output the number of percentage marks entered, the average mark and how many marks were above 40. (6 Marks} we A bank in your town updates its customers’ accounts at the end of each month. The bank offers two types of accounts: savings and checking. Every customer must maintain a minimum balance. If a customer's balance falls below the minimum balance, there is service charge of TSh. 1500/= for savings accounts and TSh 10,000/= for checking accounts. If the balance at the end of the month is at least the minimum balance, the account receives interest as follows: a. Savings accounts receive 4% interest. b. Checking accounts with balances of up to TSh. 10,000,000/= more than the minimum balance receive 3% interest; otherwise, the interest is 5%. Write a program that reads a customer's account number (int type), account type (char; s for savings, c for checking), minimum balance that the account should maintain, and current balance. The program should then output the account number, account type, current balance, and an appropriate message. [8 Marks} Question Seven DUWASA has approached you to write a program that will produce the monthly water bill for its customers. The printed bill will have the following details/output: Name of customer: Customer number: Page 12 of 13 Units consumed: Service charges=600/= Total charges: For | (one) unit, DUWASA charges 500/=. If a customer exceeds 50 units in one month, the charge per unit is 600/= a. Write program to generate DUWASA Bill for its customers [5 Marks] ». Modify the code in (a) to send the output in a file for 70 customers [5 Marks} . Write a program that will read data from the file generated in (b) and send the output to the screen [5 Marks] Organize the code segments in a, b and c in the form of functions with the main() function calling the other functions (the file output function and the file input function) [5 Marks] = Page 13 of 13, IVERSITY OF DODOMA FORMATICS AND VIRTUAL EDUCATION L OF INFORMATICS UNDERGRADUATE SUPPLEMENTARY/SPECIAL EXAMINATIONS 2017/2018 CS 110: INTRODUCTION TO HIGH LEVEL PROGRAMMING Date: 27" September, 2018 Time Allocated: 3 Hours Instructions: 1 5 This paper consists of NINE questions. Answer all questions in Section A and two questions in section B All University of Dodoma examination regulations apply. Page | of 4 ION A(60 Marks) Answer any all questions from this section Question One Vv (a) What is the difference between variables and identifiers? (2 Mark (b) What is the difference between a pointer type and an integer type? [2 Marky (c) Which of the following identifiers ARE WRONG in C++ and WHY? [8 Marks] 3id _yes O_nono — -go__startit &me_ _11g0 -60 Ttu=tu ttt888_ for case 8 (d) List five keywords in C++ and state their function [5 Marks] ¥ (©) Write a code to solve the following equation [8 Marks] ~baVoF=Fae root = ea ¥ (f) Use MTL/RAM diagrams to show variables and their status for each line of code in (e) [8 Marks] (g) Write a code to accept up-to TWENTY integers through the keyboard. At the end of its run the program shall display the biggest number entered and the smallest number entered. HINT, if you have entered 40,1, 45, 65, 8, 10, 9, 44, 78, al, 80, 400... the output shall be: The largest number entered is 400 The smallest integer entered is 1 Page 2 of 4 s} s] (6 Marks) Question Two (a) Write a code that reads in 8 positive numbers and print out the sum of 4 first even numbers and the sum of 4 first odd numbers [6 Marks} (b) Using a loop facility in C+ write a code to convert Meters from 1 to 100 into centimeters. Output the result in a tabular form as demonstrated below [6 Marks] 1 Meter = 100 Centimeters 2 Meters = 200 Centimeters 3 Meters = 300 Centimeters 10 Meters= 10000 Centimeters +Using MTL/RAM diagrams show what is happening in the variables per each line of code [8 Marks] SECTION B(40 Marks) Answer any Two questions from this section Question Four Y (a) Write a code to read scores of students of a class of 40 studying five subjects. The code will calculate the total Page 3 of 4 ee oo | scores for each student, compute the average, report the best | and the worst performers, [8 Marks} (b) Write a code [function] which will store the output from question 4(a) ina file {8 Marks} (c) Write a code {function} which will read data from the file in 4(b)and display it to the screen [4 Marks} Question Five Given a pay-slip of UDOM which contains: Names of employee, college, pay-number, salary-scale, Basic-Pay, Pay-as-you-earn, PPF, Funeral-Contribution, account-number and net-pay; [20 Marks] Write a payroll program for UDOM. Organize your code in 4 functions: one called pata_data() for data capture. Another called kokotoa_mshara () for calculations. Another called /aili_slipy() for saving the salary slip in a file and another called print_slipu() for reading the salary slip from the file to the screen Question Six Given a class of 30 students. Write a program that will capture the score in an array in the main function and other three functions: ‘one that will bubble-sort the array in descending order, another to sort it in ascending order and another to file the data in a file [20 Marks] Page 4 of 4 THE UNIVERSITY OF DODOMA COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION SCHOOL OF INFORMATICS UNDERGRADUATE UNIVERSITY EXAMINATIONS FIRST SEMESTER 2018/2019 CS 110: INTRODUCTION TO HIGH LEVEL PROGRAMMING Date: 20" February, 2019 Time Allocated: 3 Hours Instructions: 1. This question paper consists of two sections: A and B. 2. Answer ALL questions in section A and ANY two from section B 3. All University of Dodoma examination regulations apply UUM ae SECTION A (60 Marks) Question One 118 Marks) a. Briefly exalain why programming is important in your ¢ ‘areer? [3 Marks) When they arg [2 Marks) (6 Marks} b, Why programmers use #include [2 Marks} ©. With one example for each, list only four rules for naming variables [5 Marks] Questi Two [18 Marks] a Write C++ program that allow user to enter three numbers and determine the maximum and minimum number. [3 Marks} b. Use RAM diagram to dissect the code in a) above[4 Marks] ¢. Write a program that stores an integer value 20. Use pointer to change the stored value to 30, [4 Marks] d. Use RAM diagram to dissect the code in c) above [4 Marks} 2of 11 ¢ Explein the importance of commenting your codes in programming [3 Marks] Question Three [12 Marks} a. What is the output of the following program if the input is 2? [3 Marks] #include using namespace std: void funcl( ); Void func2( 5; int main() { int num; cout << "Enter | or 2:": cin >> num; cout << endl: cout << "Take "; 1) funcl( ); else if (num =='2) func2( ); else cout << "Invalid input. You must enter a | or 2" << endl; return 0; void funcl() t cout << "Programming I." < at 13m. using namespace std; int maind) aks) / intj int one[S]; int two[ 10]; for (= 0, j < 5; j++) onefj] = 5 *j +3; cout << "One contains: ": for j = 0; < 5; j++) cout <«< one[j] <<": cout << endl, for j = 0; 5 <5: j++) { twofj] = 2 * onefj} - 1; twolj + 5] = one[4 - j} + rwopjp; / cout << "Two contains; ": for Gj = 0; 7 < 10; j++) cout << twofj] <<". cout << endl; return (); 1 } ¢. Consider the following C++ code: what will be printed when alpha is 24 [3 Marks} Hincluae using namespace std; int main() int alpha; cin >> alpha; 4ofll switch (alpha % 6) case 0 alpha-- break, case 1: case 2 alpha = alpha * 2: break; case 3. break; case 4 alpha = alpha - 5; case 5, alpha++; break; default alpha = alpha / 3; i cout << alpha; return 0, , / d. What is the output of the following code assuming y=4,2=6 Hinclude using namespace std, int main() / [3 Marks} é int x,y, Z; cin>> x>>y>>z; double average = (x+y+z /3; cout<< “The average is”<< average; Sof ll ee 0 return Q; Question Four i Bs {12 Mark, Identify error(s) in the following C++ code ‘s] a. include using namespace std, [3 Maris) | int main(, { int x, y; float hisa; sum =x + y; coui<<” The sum is:” < using namespace std; int main { counter =1; put the even integers from | to [3 Marks} do Couts< counter < 100); return 0, RB #include using namespace std, int maind) { int for; char 4gender, string his name; cout<<"The Details of my Dad \n": couts<"Dad's Name: ". cin>> his name; couts<"\n"- cout<<"Dad's Age: cin>> for; cout<<"\n": cout<<"Dad's Gender; "; cin>> dgender: cout<<"\n"; cout<<"The entered details are \n": [3 Marks} cout<<"Name; "< using namespace sid: 13 Marks] int fnAdd(ind); int fnSubCint, int); int main() int sum.diff; cout<<"The sum is. sum = fnadd(4,8): cours

You might also like