CS101 Spring 2014 Midterm II (120 Minutes) Thursday, April 24Th READ and Complete the Following: • Bubble Your Scantron Only with a No

CS101 Spring 2014 Midterm II (120 Minutes) Thursday, April 24Th READ and Complete the Following: • Bubble Your Scantron Only with a No

CS101 Spring 2014 Midterm II (120 minutes) Thursday, April 24th READ and complete the following: • Bubble your Scantron only with a No. 2 pencil. • On your Scantron (shown in the figure below), bubble : 1. Your Name 2. Your NetID 3. Form letter "A" 4. Bubble the corresponding 3-digit code (shown below) for your lab section on your Scantron. Time Monday Tuesday Wednesday Thursday 9:00-10:50 101 104 108 111 11:00-12:50 102 105 109 112 1:00-2:50 106 113 3:00-4:50 103 107 110 114 Five points will be deducted if a student does not correctly record their netid on the bubble sheet. • No electronic devices, books, notes, or cheat sheets are allowed while taking this exam. • Please fill in the most correct answer on the provided Scantron sheet. • We will not answer any questions during the exam. • Each question has only ONE correct answer. • You must stop writing when time is called by the proctors. No extra time will be given after the exam ends to fill in bubble sheets with answers. • Hand in both these exam pages and the Scantron. • DO NOT turn this page UNTIL the proctor instructs you to. Given the following Unix directory tree, answer the next 3 questions. 1. The jsmith directory is your home directory. After entering pwd at the Unix prompt, you get the following output: /home/jsmith/labs/lab2 Which of the following commands, when entered at the Unix prompt, will change your current working directory to the mp1 directory? (a) cd ../../mp1 (b) cd ~/jsmith/mp1 (c) cd ../mp1 (d) mv ./..mp1 2. If your current working directory is changed to the jsmith directory, which of the following commands, when entered at the Unix prompt, will copy every file with .c suffix from the mp1 directory to the handin directory? (a) cp *.c mp1 handin (b) cp mp1/.c handin (c) cp .c mp1 handin (d) cp mp1/*.c handin 3. If your current working directory is the jsmith directory, which of the following commands, when entered at the Unix prompt, will delete the labs directory and everything in the labs directory? (a) del -d labs (b) rm labs (c) rm -r labs (d) rm -f labs 4. Suppose there is a sub-directory named student in your current working directory. Which of the follow- ing commands, when entered at the Unix prompt, will rename the student directory as assignment? (a) rn student assignment (b) mv student assignment (c) mv assignment student (d) cp assignment student 5. Suppose you have a C programming language file named test.c in your current directory. The file test.c contains a complete C program that has no errors. Which of the following commands will successfully compile and run the C program found in the file test.c ? (a) ./test.c (b) gcc test.c ./test (c) gcc test.c -o test test.c (d) gcc test.c ./a.out 6. Which of the C programs shown below will not compile successfully? (a) #include <stdio.h> void main(void) { int a = 7; scanf("%i", &a); printf("%i is my favorite number\n",a); } (b) #include <stdio.h> void main(void) { int a = 7; printf("%d is my favorite number\n",a); } (c) #include <stdio.h> void main(void) { int 5+2 = a; printf("%i is my favorite number\n",a); } (d) #include <stdio.h> void main(void) { int a = 5, b = 2; printf("%i is my favorite number",a+b); } 7. Which of the C programs shown below will not produce the following result when compiled and then run at the Unix terminal? 1 is my favorite number! (a) #include <stdio.h> void main(void) { int a = 5/3; printf("%i is my favorite number!\n", a); } (b) #include <stdio.h> #define M 1 void main(void) { printf("%i is my favorite number!\n", M); } (c) #include <stdio.h> void main(void) { int a = 1; printf("%a is my favorite number!\n", a); } (d) #include <stdio.h> void main(void) { int a = 5; printf("%i is my favorite number!\n", a%2); } 8. The following C program compiles and runs without errors. #include <stdio.h> void main(void) { int a = 3; int i = 0; char L = ’c’; /* Hint: Look very carefully at the "for" statement below. */ for( i = 0; i < a; i++); printf("W"); while(i<5) { printf("O"); i++; } printf("%c",L); } What is the output the program produces? (a) WOOc (b) WWWOOL (c) WWWOOc (d) WOOOOOL 9. The following C program compiles and runs without errors. #include <stdio.h> void main(void) { int hours; printf("How many hours a day do you spend on homework? "); scanf("%i", &hours); if(hours >= 5) printf("Heavy workload."); else { if((hours < 5) && (hours >= 0)) { if(hours >= 3) printf("Normal workload."); if(hours > 0) printf("Light workload."); else printf("No workload."); } } } What is the output if the user types an input of integer value 3 where prompted with "How many hours a day do you spend on homework? " ? (a) Normal workload.No workload. (b) Normal workload.Light workload. (c) Heavy workload. (d) Normal workload. 10. The following C program compiles and runs without errors. #include <stdio.h> void main(void) { int num; int remain; printf("Please enter an integer less than 10 but greater than zero: "); scanf("%i", &num); if((num < 10) && (num > 0)) { switch(num % 4) { case 0: remain = 0; printf("Integer value is %i with remainder %i.", num, remain); break; case 1: remain = 1; printf("Integer value is %i with remainder %i.", num, remain); case 2: remain = 2; printf("Integer value is %i with remainder %i.", num, remain); break; default: remain = 3; printf("Integer value is %i with remainder %i.", num, remain); } } else printf("Error: Please rerun the program with a valid input."); } What is the output if the user types an input of integer value 5 where prompted with "Please enter an integer less than 10 but greater than zero: " ? (a) Integer value is 5 with remainder 1. (b) Integer value is 5 with remainder 1.Integer value is 5 with remainder 2. (c) Error: Please rerun the program with a valid input. (d) Integer value is 5 with remainder 1.Integer value is 5 with remainder 1. 11. The following C program compiles and runs without errors. #include <stdio.h> #include <string.h> int x = 1; int function1 (int); void main(void) { int result = function1(x+1); printf(" %i %i\n", result, x); } int function1 (int x) { int y = 4; return x * y; } What is the output the program produces? (a) 8 1 (b) 8 2 (c) 4 1 (d) 4 2 12. The following C program compiles and runs without errors. #include <stdio.h> void main(void) { int i; int j; int count; count = 0; for(i = 0; i < 2; i++) { for(j = 2; j < 4; j++) { printf("(%i %i) ", i, j); count = count + 1; } j = 1; printf("(%i %i) \n", i, j); } printf("Count is: %i", count); } What is the output the program produces? (a) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 4 (b) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) Count is: 6 (c) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) (2 2) (2 3) (2 1) Count is: 6 (d) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) (2 2) (2 3) (2 4) (2 1) Count is: 9 13. The following C program compiles and runs without errors. #include <stdio.h> void main(void) { int i; int j; int count; count = 0; for(i = 0; i < 2; i++) { j = 2; while(j < 4) { printf("(%i %i) ", i, j); j++; } j = 1; count = count + 1; printf("(%i %i) \n", i, j); } printf("Count is: %i", count); } What is the output the program produces? (a) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 4 (b) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 2 (c) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) (2 2) (2 3) (2 4) (2 1) Count is: 3 (d) (0 2) is displayed on the computer screen infinitely many times. 14. Which of the following programs does NOT produce the following output? 0.0, 7.5, 15.0, 22.5, (a) #include <stdio.h> void main(void) { double i = 2.0; int j; i = (15 * 2.0) / 4; for(j = 0; j < 4; j++) printf("%5.1lf, ", j * i); } (b) #include <stdio.h> void main(void) { double i = 2.0; int j = 0; i = (15 * 2.0) / 4; while(j < 4) printf("%5.1lf, ", j * i); } (c) #include <stdio.h> void main(void) { double i = 2.0; int j = 0; i = (15 * 2.0) / 4; do { printf("%5.1lf, ", j * i); j++; }while(j < 4); } (d) #include <stdio.h> void main(void) { double i = 2.0; int j = 0; i = (15 * 2.0) / 4; while(j < 4) { printf("%5.1lf, ", j * i); j++; } } 15. The following C program compiles and runs without errors. #include <stdio.h> #include <string.h> void main(void) { int i; char mystr[] = {’d’,’o’,’g’,’!’,’\n’,’\0’,’c’,’a’,’t’,’?’,’\n’}; for (i=0; i < strlen(mystr); i++) printf("%c",mystr[i]); } What is the output the program produces? (a) dog! cat? (b) dog! cat? (c) dog! (d) dog! \0cat? 16. The following program compiles and runs without errors. What is the output this program produces? #include <stdio.h> void swap2(int* x) { int temp = x[0]; x[0] = x[1]; x[1] = temp; } void swap1(int x, int *y) { int temp = x; x = *y; *y = temp; } void main(void) { int a[2] = {1, 2}; int b[3] = {4,5,6}; swap1(a[0], a); printf("%i %i \n", a[0], a[1]); swap2(&b[1]); printf("%i %i %i\n", b[0], b[1], b[2]); } (a) 2 1 5 4 6 (b) 1 2 5 4 6 (c) 2 1 4 6 5 (d) 1 2 4 6 5 17.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    21 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us