Quiz #1

Instructions: Answer each question. If the question asks you for output from a program, you must give the output character-for-character, including exactly the right number of blanks, except for the new-line, which you must omit.

Question 1   Fill in the Blank (1 points)
  Question: What is the output of the following program?
#include <stdio.h>
int main (int argc, char const *argv[]) {
  int i, j =3;
  for (i = 0; i < 10; i+=2); j = j + 1;
  fprintf (stdout, "i = %3d, j = %3d.\n", i, j);
  return 0;
}
 
     

Question 2   Fill in the Blank (1 points)
  Question: What is the output of the following program, assuming it is compiled to produce an executable file named "a.out" and executed by the shell command "./a.out Hello"?
#include <stdio.h>
#include <ctype.h>
int main (int argc, char const *argv[]) {
  char buf[10];
  char const *p;
  p = argv[0];
  for (p = argv[0]; *p; p++) {
    buf [p - argv[0]] = toupper (*p);
  }
  buf[p - argv[0]] = '\0';
  fprintf (stdout, "buf = \"%s\".\n", buf);
  return 0;
}
 
     

Question 3   Multiple Choice (1 points)
  Question: What is the output of the following program?

#include <stdio.h>

char *p;

int main (int argc, char const *argv[]) {
strcpy (p, "Hello there!\n");
fprintf (stderr, "%s", p);
return 0;
}


 
    Hello there!
Hello there!\n
"Hello there!\n"
%s
"%s"
Segmentation fault (core dumped)
the program has syntax error(s)
 

Question 4   Fill in the Blank (1 points)
  Question: What is the output of the following program?

#include <stdio.h>


int equal (int x, int y) {
if (x = y) return 1;
return 0;
}

int main (int argc, char const *argv[]) {
fprintf (stderr, "%1d", equal (3, 4));
return 0;
}

 
     

Question 5   Fill in the Blank (1 points)
  Question: What is the output of the following program?

#include <stdio.h>


int x[] = {3, 4, 5, 6, 0, 7};

int main (int argc, char const *argv[]) {
char *p = (char *) x;
p += sizeof (int);
*(int*) p = 8;
for (p = (char *) x; *p; p+= sizeof (int)) printf ("%2d", *(int *) p);
return 0;
}

 
     

Question 6   Fill in the Blank (1 points)
  Question: What is the output of the following program?
#include <stdio.h>


#define xx aa
#define aa bb
#ifdef aa
#define bb(x) "hello" x
#else
#define aa(x) "goodbye" x
#endif

int main () {
printf (aa(" again"));
return 0;
}

 
     

Question 7   Fill in the Blank (1 points)
  Question: What is the output of the following program?

#include <stdio.h>

int main () {
int n = 0xffffff;
printf ("%10x", (n % (n << 16)) >> 4);
return 0;
}


 
     

Question 8   Fill in the Blank (1 points)
  Question: What is the output of the following program?

#include <stdio.h>


int main () {
int n = 2;
switch (n) {
case 1:
case 2:
printf ("first;");
case 3:
case 4:
printf ("second;");
}
return 0;
}

 
     

Question 9   Fill in the Blank (1 points)
  Question: What is the output of the following program?

#include <stdio.h>

int main () {
int n;
int m;
n = 1 + 2 + 8; m = 4 + 8 + 16;
printf ("%2d %2d %2d %2d", n && m, n || m, n & m, n | m);
return 0;
}



 
     

Question 10   Ordering (1 points)
  Question: Give the names of the Unix shell commands to perform the following operations:


  • copy a file

  • remove a file from a directory

  • rename a file

  • change working directory

  • compile a program

  • change access permissions on a file





 
   
 
 
  rm
  cd
  cp
  mv
  chmod
  gcc