National University of Computer & Emerging Sciences, Karachi
Fall -2021 (School of Computing)
                        Final Exam, 09:00 am - 12:00 pm, Wed 29th December, 2021
  Course Code: CS1002                          Course Name: Programming Fundamentals
  Instructors: Dr. Abdul Aziz, Dr. Farrukh Shahid, Dr. Murk Marvi, Mr. Basit Ali, Ms.Abeer Gauher, Mr. Hamza Ahmed, Ms.
  Attiya Jokio, Ms. Sobia Iftikhar. Ms. Sumaiya.
  Student ID:                                  Section:
Instructions:
        Attempt all questions on answer sheet, including MCQ s.
        Return the question paper and make sure to keep it inside your answer sheet.
        Read each question completely before answering it. There are six questions and six pages.
        In case of any ambiguity, you may make assumption. However, your assumption should not contradict any
        statement in the question paper
        Do not write anything on the question paper (except your ID and group).
                                                                                                   Total Points: 100
Question 1: Multiple choice questions.                                                       (1 mark each= 10 marks)
1. Maximum number of elements in the array declaration                     are:
       a)   28                    b)     32                              c)     35                        d)      40
2. A pointer to a pointer is a form of:
       a)      Multiple indirections                                     c)      Both a and b
       b)      A chain of pointers                                       d)      None of these
3. An expression contains relational, assignment and arithmetic operators. If Parenthesis are not present, the
order will be:
       a)      Assignment, arithmetic, relational                 c)     Assignment, relational, arithmetic
       b)      Relational, arithmetic, assignment                 d)     Arithmetic, relational, assignment
4. p++ executes faster than p+1 because:
       a)    p uses temparory memory                                     c)      ++ is faster than +
       b)    p++ is a single instruction                                 d)      None of these
5.                                                                     What value does testarray[2][1][0] in the
sample code above contain?
      a)     11                           b)     7                       c)      5                        d)      9
6. What is printed when the sample code above is executed?
      int y[4] = {6, 7, 8, 9};
      int *ptr = y + 2;
      printf("%d ", ptr[ 1 ] );
      a)       6                    b)    7                              c)      8                        d)      9
7. What is printed when the sample code above is executed?
 #include <stdio.h>
 void reverse(int i);
 int main()                                                  a)      12345
 { reverse(1); }                                             b)      54321
  void reverse(int i)                                        c)      Compilation error
   {    if (i > 5)                                           d)      65432
                 {return ;}
        reverse((i++, i));
        printf("%d ", i); }
8. What is printed when the sample code above is executed?
#include <stdio.h>                                                       a)     10 10 10
int main()
{                                                                        b)     100xaa54f10
   int x = 10, *y, **z;                                                  c)     Run time error
                                                                         d)     No Output
     y = &x;
     z = &y;
     printf("%d %d %d", *y, **z, *(*z));
     }
9. Which of the following is executed by Preprocess?
   a) #include<stdio.h>                                              c) void main(int argc , char ** argv)
   b) return 0                                                       d) None of above
10. If x is an array of integer, then the value of &x[i] is same as:
      a) &x[i-1] + sizeof (int)                                      c) x+i
      b) x + sizeof (int) * i                                        d) none of these
Question 2. Show the output.                                                                (1 mark each= 10 marks)
What will be the output?
1.       main(){                                                2.      main()
               char *p="Hello world";                                   {
               int *q;                                                  int color=2;
               p++;                                                     switch(color)
               q = (int *)p;                                            {
               q++;                                                            case 0: printf("Black");
               printf("%s%s",p,q); }
                                                                               case 1: printf("Blue");
                                                                               case 2: printf("Green");
                                                                               case 3: printf("Aqua");
                                                                               default: printf("Other");
                                                                        }}
3.       main()                                                 4. main()
         {                                                      {
         char s[ ]="man";                                               int const * p=5;
         int i;                                                         printf("%d",++(*p));
         for(i=0;s[ i ];i++)                                    }
         printf("%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
         }
5. main()                                                  6. int main()
  {                                                           {
   int array[10] = {3, 0, 8, 1, 12, 8, 9, 2, 13, 10};         int arri[] = {1, 2 ,3};
   int x, y, z;                                               int *ptri = arri;
  x = ++array[2];                                             char arrc[] = {1, 2 ,3};
  y = array[2]++;                                             char *ptrc = arrc;
  z = array[x++];                                             printf("sizeof arri[] = %d ", sizeof(arri));
                                                              printf("sizeof ptri = %d ", sizeof(ptri));
  }                                                           printf("sizeof arrc[] = %d ", sizeof(arrc));
                                                              printf("sizeof ptrc = %d ", sizeof(ptrc)); }
7. main()                                                  8. main()
   {
                                                             {
   int val = 1;
                                                             char str1[] = "Hello. How are you?";
                                                             char str2[21];
  do {
                                                             int pos;
     val++;
                                                             for(pos=0; pos<21; pos++);
     ++val;
                                                            {
  } while (val++ > 25);
                                                               str2[pos] = str1[pos];
  printf("%d\n", val);}
                                                            }
                                                            printf("str1: %s, str2: %s", str1, str2); }
9. main()
                                                            10. main()
  {
                                                             {
  char *_ptr = malloc(100);
                                                                 int x = 5, y = 10;
  if(_ptr!=NULL)
                                                                 int const *p = &x;
  free(_ptr);
                                                                 *p = 15;
  free(_ptr); }
                                                                 printf("%d", x); }
Question 3. Ali is playing a game of cards. This is a special type of cards in which cards are a 3 digit number
(i.e. 100 - 999). He has to sort the cards in ascending order. However, sorting will be done on the sum of the
digits. Make a c program to sort his cards, take user input for number of cards. Taking input of cards number
should be a function named get_data(). Sorting should be another function sort() and displaying the final
result should be another function display().                                                        (20 marks)
Expected Input:                                             Expected Output:
How many cards do you have? 5                               Sorted card 1 : 124
Enter number of card 1:        291                          Sorted card 1 : 371
Enter number of card 2:        124                          Sorted card 1 : 291
Enter number of card 3:        371                          Sorted card 1 : 574
Enter number of card 4:        574                          Sorted card 1 :189
Enter number of card 5:        189
Question 4. Someone has asked you to design a special purpose calculator with the following two options.
   a. Solve an equation involving combination of four (+, -, *, /) operators and maximum 100 operands.
      Assume that all the operators have same precedence and equation is supposed to be solved from left
      to right. For example, 3+34.5/2*60-22 is an equation, and it consists of six operands and five
      operators. In order to store all the operands and operators of the equation, define an array of type
       struct data
      array store the first two operands (3), (34.5) and the first operator (+). At the next index of the array,
      store the result of the last operation (37.5), the next operator (/), and the next operand (2). Call a user-
      defined function basic( ) in order to perform a given arithmetic operation on two operands. Continue
      this until all the operands and operators of the equation are entered and you get the final result.
   b. Obtain the dot product of two vectors of same size. Note that if A=[1,2,5,2,1] and B=[4,5,6,3,10], then
      the dot product can be obtained as A . B = 1*4 + 2*5 + 5*6 + 2*3 + 1*10 = 60.
In main (), ask the user whether she wants to solve an equation or dot product. The user should press a for
choosing equation and b for choosing dot product. If the user enters any character other than a or b the
program should terminate after printing an invalid input message.
If the user chooses a, then ask how many operands are there in the equation. Store the operands and
operators entered by the user in an array of type struct data. In case the user enters an invalid operator or
operands, print the message of invalid input. Otherwise, call a user defined function basic( ) to get the
required result. Print the result by calling display( ) function from main( ). The program ends only if the user
wants to terminate it.
If the user chooses dot product by pressing b, then ask her to enter the elements of two arrays. Call dot( )
function in order to calculate the dot product of two arrays. Print the result in main(). The program ends only
if the user wants to terminate it.                                                                  (20 marks)
For defining basic( ), display( ), and dot( ) functions, please follow the following details.
   a. float basic(float, float, char op);
   b. void display(const struct data [ ], int);
   c. double dot(const double [ ], const double [ ], int);
                                          Fig 1.1: Sample Output 1
                                           Fig 1.2: Sample Output 2
Question 5. Suppose we have the following character array:
Required: You are required to write a code for the following operations:           (5 marks each = 20 marks)
   a. Reverse the given string without using any additional array or even an extra variable. The resultant
                                                             .
   b.
   c. Wildcard Pattern Matching: Given a string and a pattern containing wildcard characters, i.e., * and ? ,
      where ? Can match to any single character in the string and * can match to any number of characters
      including zero characters, design an efficient algorithm to check if the pattern matches with the
      complete string or not.
      Input: string =             pattern =                      Input: string =             pattern =
      Output: Match                                             Output: No Match
      Input: string =          pattern =
      Output: Match
   d. Write a recursive function to reverse the input words.
      Input:
      Output: