Quiz C3-1 | CS 2113 Software Engineering - Spring 2021

Quiz C3-1

Submit your answers via this Google Form

Question 1

Below are the outcomes of two games of checkers in string form, but someone has
modified the program to change the boards! Before the evil code, it was a tie as
each player one once, select who won after the evil code runs.

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

int main() {
  char *game_1[3] = {
    "o|x| ",
    "o|o|x",
    "x|x|o"};
  char *game_2[3] = {
      "x|x|o",
      "o|x| ",
      "o|x|o",
  };
  //Begin Evil Code
  for (int i = 0; i < 3; i++) {
    char **tmp = &game_1[i];
    game_1[i] = game_2[i];
    game_2[i] = *tmp;
  }
  //End Evil Code

  printf("The winner is...");
}

Question 2

What goes in the point indicated below in order to print out the variable c.

#include <stdio.h>

int main() {
  char *str = "Why so many different types?";
  char *ptr = &str[5];
  unsigned long b = (unsigned long)ptr + 5;
  unsigned long *c = &b;
  printf("%c",/* What goes here? */);
}

Question 3

Consider the following program. What is the output?

#include <stdio.h>
#include <string.h>
     int main(){
       int a[2][3] = { {10,20,30}, {5,6,7} };

       int * p = &((int *) a)[4];
  
       p[1] = 42;
  
       for(int i=0;i<2;i++){
         for(int j=0;j<3;j++){
           printf("%d ",a[i][j]);
         }
       }
       printf("\n");  
     }


Question 4

Consider the following program, what is the output?

int main(){
  int * a[] = { {10,20,30}, 
                {5,6,7}   };

  int ** p = a;
  int * q =  p[1];
  
  q[1] = 42;
  
  for(int i=0;i<2;i++){
    for(int j=0;j<3;j++){
      printf("%d",a[i][j]);
    }
  }
  printf("\n");  
}

Question 5

Consider the following program, what is the output?

int main(){

  int a[] = {0x42434445, 
             0x20212223};
             
  short * b = (short *) a;
  
  //prints the two bytes of the short as a hexaxdecimal
  // like 0xbeef or 0xdead
  
  printf("0x%0hx",b[0]);
  
  //so what is the short in b[0] as a hexadecimal?
}