We aim to provide you with our previous exams from recent months. This will assist you in preparation and offer a clearer understanding of the structure, content, and types of questions.

This is the original exam from 12.12.2023

Introduction

<aside> ⚠️ This exam consists of three parts: i) three code analysis questions (without an online IDE), ii) one debugging question (with an online IDE), and iii) a coding example (again, with an online IDE).

You are writing this exam in Exam TeachCenter and Safe Exam Browser, which means you have to stay within this exam environment. All the code provided is in the C programming language. Be as precise as possible with your answers. Questions 1 - 3 have to be answered completely correctly (which means you have to write the exact output), since they are evaluated automatically.  You can assume that all provided code examples can be compiled correctly and they won’t produce a runtime error. To make the code snippets easier, we might not put it in a main function or include headers. You can assume that the code is working. For questions 4 and 5 you can use OnlineGDB. Please do not forget to copy the code from OnlineGDB into the input field. OnlineGDB does not store your answers. If the code is not in Exam TeachCenter by the end of the exam (which will be submitted automatically after the time is over), you will get 0 points on this question.

Important: After submitting the code analysis questions (part i) you cannot navigate back to these questions again.

All the best for the exam!

</aside>


Q1) (2 Points)

What is the Output of this code snippet?

typedef struct _Item_ 
{
  char product_[50];
  short quantity_;
  float price_;
  struct _Item_* next_;
} Item;

int main(void) 
{
  Item bread = { .product_ = "Bread", .quantity_ = 2, .price_ = 1.5, .next_ = NULL };
  Item milk = { .product_ = "Milk", .quantity_ = 1, .price_ = 2.0, .next_ = &bread };
  Item eggs = { .product_ = "Eggs", .quantity_ = 12, .price_ = 3.0, .next_ = &milk };

  float price = .0f;

  Item* current_item = &bread;
  
  while (current_item != NULL) 
  {
    price += current_item->price_;
    current_item = current_item->next_; 
  }
  
  printf("%.2f", price);
}


Q2) (2 Points)

What is the Output of this code snippet?

int matrix[4][4] = 
{
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 0, 1, 2},
  {1, 3, 6, 1}
};

int sum = 0;

for (size_t i = 0; i < 4; i++) 
{
  int *ptr = &matrix[i][2];
  sum += *ptr;
}

printf("%d", sum);

Q3) (2 Points)

You are given the following CSV file (superheroes.csv):

SuperheroName,Franchise\\n
Iron Man,Marvel\\n
Wonder Woman,DC\\n
Spider-Man,Marvel\\n
Batman,DC\\n
Thor,Marvel\\n
Superman,DC\\n
Black Widow,Marvel

What is the Output of this code snippet?

FILE *file = fopen("superheroes.csv", "r");
if (file == NULL)
    return -1;

int lines = 0;
char c;

fseek(file, 0, SEEK_SET);

for (int i = 0; i < 1; i++)
{
  while ((c = fgetc(file)) != '\\n');
}

while ((c = fgetc(file)) != EOF) 
{
  if (c == '\\n') 
  {
    lines++;
  }
}

printf("%d", lines);

fclose(file);