MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Similar documents
Quick review pointer basics (KR ch )

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Multidimension array, array of strings

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Dynamic memory allocation (malloc)

Procedural programming with C

Fundamental of Programming (C)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Lecture 04 Introduction to pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Chapter 11 Introduction to Programming in C

Arrays and Pointers (part 1)

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Chapter 11 Introduction to Programming in C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

CS 61c: Great Ideas in Computer Architecture

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

C0MP1911 Final Exam 1337 Computing 1

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring 2015

CS 0449 Sample Midterm

Introduction to C. Systems Programming Concepts

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Dynamic Memory Allocation and Command-line Arguments

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

First of all, it is a variable, just like other variables you studied

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

COMPUTER APPLICATION

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 11 Introduction to Programming in C

Chapter 3: Arrays and More C Functionality

Arrays and Pointers (part 1)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Chapter 11 Introduction to Programming in C

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Programming for Engineers Iteration

Government Polytechnic Muzaffarpur.

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Pointers. Pointers. Pointers (cont) CS 217

ECE264 Fall 2013 Exam 1, September 24, 2013

Operators and Expressions:

C Programming

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS16 Exam #1 7/17/ Minutes 100 Points total

ME964 High Performance Computing for Engineering Applications

Language comparison. C has pointers. Java has references. C++ has pointers and references

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Practice Sheet #07 with Solutions

CS 107 Lecture 2: Bits and Bytes (continued)

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Conditional Statement

EC312 Chapter 4: Arrays and Strings

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Fundamental of Programming (C)

Chapter 12 Variables and Operators

Information Science 1

Exercise Session 2 Simon Gerber

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

FORM 2 (Please put your name and form # on the scantron!!!!)

EM108 Software Development for Engineers

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Pointers and Arrays 1

CSCI 2132 Final Exam Solutions

Lecture 3. More About C

BSM540 Basics of C Language

COMP 2001/2401 Test #1 [out of 80 marks]

Multiple Choice Questions ( 1 mark)

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

C: How to Program. Week /Mar/05

Bristol Institute of Technology

Arrays, Pointers and Memory Management

PROGRAMMAZIONE I A.A. 2017/2018

Computers Programming Course 5. Iulian Năstac

Final CSE 131B Spring 2004

Transcription:

MIDTERM TEST EESC 2031 Software Tools June 13, 2017 Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes This is a closed-book test. No books and notes are allowed. Extra space for answers can be found at the end of the test booklet. Assume that o size of char is 1 (byte); o size of short integer is 2 (bytes); o size of integer is 4 (bytes); o size of long integer is 8 (bytes); o size of float is 4 (bytes); o size of double is 8 (bytes); Also assume that the size of a pointer is 8 (bytes). Question Value Mark 1 14.5 2 12 3 23.5+1.5 bonus TOTAL 50 +1.5 bonus Good Luck

Jun 13, 2017 (Tuesday) Name: Student ID: Page 3 of 15 Question 1 (14.5 pts) 1.1 (0.25 pt) For the following code fragment, what is the output of the printf() statement? int x = 9, y = 13, z = 3; printf("z: %d\n", z *= y + x + 2 ); z: 72 1.2 (1 pt) What is the output of the printf() statement? int x = 2, y = x << 2; printf("%d %d %d %d\n", x, y, x y, x&y); 2 8 10 0 1.3 (1 pt) What is the output of the printf() statement? int x = 13, y = z = 10; y = x++; z += y++; printf("x:%d y:%d z:%d\n", x,y,z); 14 14 23 1.4 (1 pt) What is the output of the printf() statement? int x = 3; int y = 4; printf("%d %d %d %d\n", x y, x & y, x y, x && y); 1.5 (0.5 pt) What is the output of the printf() statement? int a = 6; int c = 4; float d = 1.1; short s =1; 7 0 1 1 printf("x:%.3f y:%.3f\n", s*a/c*d, d/c*a*s); 1.100 1.650 1.6 (0.5 pt) In 1.5, when d/c*a*s is evaluated, how many type conversions are performed? What is the type of the expression? three float 1.7 (0.5 pt) Recall that in ANSI-C, implicit type conversions are preformed on the following occasions 1) When the operands in an arithmetic or logical expression don t have the same type. 2) When the type of the expression on the right side of an assignment doesn t match the type of the variable on the left side.

3) When the type of an argument in a function call doesn t match the type of the corresponding parameter. 4) When the type of the expression in a return statement doesn t match the function s return type. On which of the above occasion(s), is type promotion performed, in which the type of the operand with the narrower type is converted to the type of the other operand? a. On 1) b. On 2) c. On 3) and 4) d. All of the above. 1.8 (1.5 pt) Consider the following ANSI-C program, where the programmer made typos on line 6 and 8, and also missed a else on line 8 #include <stdio.h> int main (){ int in; printf("enter an integer: "); scanf ("%d", &in); if (in = 1) /* this is line 6 */ printf("entered one\n"); if (in = 2) /* this is line 8 */ printf("entered two\n"); else printf("entered others\n"); return 0; What happens when you try to compile and run the program with input 2? (chose a. or b.) a. The program does not compile, because b. The program compiles, output is Entered one Entered two What happens when you try to compile and run the program with input 36? (chose a. or b.) a. The program does not compile b. The program compiles, output is Entered one Entered two What happens when you try to compile and run the program with input 1? (chose a. or b.) a. The program does not compile b. The program compiles, output is Entered one Entered two

Jun 13, 2017 (Tuesday) Name: Student ID: Page 5 of 15 1.9 (0.25pt) Recall that a gcc compilation process contains three stages. The correct order is a. Linking Pre-processing Compiling b. Pre-processing Compiling Linking c. Compiling Pre-processing Linking d. None of the above 1.10 (1.5 pt) Consider the following statements. For each of the statements, if the right hand side of = is a valid integer literal in ANSI-C, then specify the decimal value of x. If the right hand side of = is not a valid integer literal, specify invalid. int x = b00101101; invalid int x = 014; int x = 0X3f; 63 int x = 032L; int x = 0xfFG; invalid int x = 0392; 12 26 invalid 1.11 (3.75 pt) Suppose flags is an integer variable with some value. What happens to flags in each of the following 3 statements, in terms of turning on/off or keeping the bits of flags? Denote the right-most bit as bit 0, the 2 nd right-most bit as bit 1 1) flags = flags (1 << 6); turn on bit 6 (right most is bit 0), keep other bits 2) flags = flags ~(1 << 4); keep bit 4, turn on all other bits 3) flags = flags & 0177; keep right-most 7 bits, turn off all other bits In statement 1) and 2) above, can any of the parentheses be removed without affecting the meaning and value of the expression? If yes, specify which one(s) and briefly explain why. parenthesis in 1) can be removed. Because << has higher precedence than, so 1 << 6 is evaluated before anyway Now suppose you need an int mask whose binary representation is 000...000101001. How do you declare it in ANSI-C code? List 3 ways of doing it. int mask = 41 int mask = 051 int mask = 0x29 Other slu: int mask = 1 <<5 1 << 3 1 int mask = 9 1 << 3 anything evaluated to 41 decimal What happens to flags in the following two statements, in terms of turning on/off or keeping bits of flags? Denote the right-most bit as bit 0, the 2 nd right-most as bit 1... flags = flags mask turn on bits 0, 3, 5, keep others flags = flags & mask keep bit 0, 3, 5, turn off all other bits

1.12 (1.25 pt) For each of the following array declaration and initializations, determine if the statement is legal in ANSI C. If it is illegal, write illegal. If it is legal, then list (the value of) all the elements in the array, separated by comma or space. int k[3] ={1,5,3,2,25; illegal int k[5] = {1; 1 0 0 0 0 int k[6] = {1,4,5; 1 4 5 0 0 0 int k[] ={1,5,3,2,25; 1 5 3 2 25 int k2[] = k; illegal 1.13 (1.5 pt) Given the statement char messages[3][20] = {"Hello", "Hi", "There"; What is the value of sizeof(messages)? 3 * 20 * 1 = 60 (bytes) write a single C statement to change "Hi" to "How are you" strcpy (messages[1], How are you ) or sprintf(messages[1], %s, How are you ) or sprintf(messages[1], How are you ) write a single C statement to change 'h' in "There" to 'H' messages [2][1] = H or messages[2][1] =toupper(messages[2][1]); Question 2 (12 pt) 2.1 (1 pt) Consider the following ANSI C program. #include<stdio.h> void afunction (float input){ input = input * 2.0; int main(){ float input; scanf("%f", &input); afunction(input); printf("%.3f\n", input); Now suppose the program compiles and the user enters 7.4 What is the output of the program? 7.400 Call by value. The formal argument of function happens to have the same name as the actual argument but they are two variables. The former is a copy of the latter.

Jun 13, 2017 (Tuesday) Name: Student ID: Page 7 of 15 2.2 (1 pt) Consider the following function definition in ANSI C. /* this function initializes argument array arr s first n elements to 0 */ void cleararr (int arr[], int n){ int index; while (index < n){ arr[index++] = 0; Do you think that this function will work as expected? Yes No. The reason is: index is a local variable, which is initialized by the compiler to a garbage value, not 0 need to initialize to 0 explicitly. 2.3 (1pt) Consider the following ANSI C program. #include<stdio.h> #define N 4 void afun(); int main(int argc, char *argv[]) { int k; for(k=0; k<n; k++) afun(); return 0; void afun(){ int static counter; int y = ++counter; printf("%d ", y); What are the outputs of the program? 1 2 3 4 2.4 (1 pt) Describe the usage(s) of key word static for global variable in ANSI-C. Limit the scope to the file in which it is defined. That is, make it not accessible to the other files.

2.5 (2pt) Consider the following ANSI C program. #define N 4 #define INCRE(x) x * x * x #define DECRE(x) x / 2 int main(){ When you invoke the pre-processor, using gcc -E, what is the pre-processed code of main, i.e., the output generated by the preprocessor (and sent as input to the compiler)? /* use marcos */ int i = N + 6; int j = DECRE(i); int k = INCRE(j + 10); int main(){ int i = 4 + 6; int j = i / 2; int k = j + 10 * j + 10 * j + 10 Now we compile and run the program. At the end of main, what is the value of i, j and k? i: 10 j: 5 k: 115 2.6 (1.5pt) What are the outputs of the 2 printf() statements in the following code fragment? char msg [] = "Hello"; printf("%d %d", sizeof(msg), strlen(msg)); char msg2[10] = "Hello"; printf("%d %d", sizeof(msg2), strlen(msg2)); 6 5 10 5 2.7 (1.5pt) What are the outputs of the 2 printf() statements in the following code fragment? char msg [] = "Hello the world"; printf("%s %d %d", msg, sizeof(msg), strlen(msg)); strcpy(msg, "Hi"); Hello the world 16 15 printf("%s %d %d", msg, sizeof(msg), strlen(msg)); Hi 16 2

Jun 13, 2017 (Tuesday) Name: Student ID: Page 9 of 15 2.8 (3 pt) Consider the following ANSI C program. int main(){ char name[60]; int age; float wage; printf("enter name, age and wage: "); scanf("%s %d %f", name, &age, &wage); Explain briefly: printf("%s %d %f", name, age, wage); Briefly explain: Why does function scanf() require the use of & for age and wage (i.e., &age and &wage)? (FYI: if you use age and wage without &,you get segmentation fault and the program crashes.) Scanf needs to change the value of its parameter. Since c is call/pass by value, to change/set the value of age and wage, scanf needs the address/pointer of the variables, and thus &age, &wage. Why don t we need to use & for name in scanf()? (FYI: the program won t compile if you use &name.) In C, array name contains the address of the first elements arr=&arr[0]. So we just pass it to the function without & (actually it is wrong to pass & for arrays) Why don t we need & for age and wage in printf()? To output the values of age and wage (rather than changing them), pass-by-value is sufficient. Just get a copy of the values of age and wage and output them. Question 3 Pointers and arrays (23.5 + 1.5 pt) 3.1 (4 pt) Compete the following program so that the output of main is a: 600 b:18. That is, values of a and b are swapped and then the swapped values are doubled.

/* This function double the pointee of arguments, and then call another function to swap the (doubled) values */ void increswap(int *px, int *py) { /* double the values of pointees of px and py */ * px *=2; * py *=2; /* now call function swap() to swap the pointees. swap( px, py ); /* this function swap the pointees of the two pointers */ void swap ( int *p1, int *p2 ) { int tmp = *p1; *p1 = *p2; *p2 = tmp; void main( ) { int a=9, b=300; increswap( &a, &b ); printf("a:%d b:%d", a, b); /* should get a:600 b:18 */ 3.2 (2 pt) What is the output of the following ANSI C program? #include<stdio.h> main(){ What are the two types of testing method and what are the main differences? int x = 199, y = 22; int *ip, *ip2,**pip; ip = &x; pip = &ip; ip2 = ip; y = **pip; (*ip2) *= 10; ip = &y; (** pip)++; (*ip2) -= 10; printf("x:%d y:%d\n", x,y); x: 1980 y: 200

Jun 13, 2017 (Tuesday) Name: Student ID: Page 11 of 15 3.3 (1.5 pt) Consider the following ANSI C code fragment. int arr [] = {1,2,3,4,5,6; int * p = arr; int i = arr[3]; In addition to arr[3], what are the other ways to access the same element of arr? List 3 ways. int i = *(p + 3) int i = * (arr + 3) int i = p[3] 3.4 (2.25 pt) Consider the following ANSI C program. main(){ int i; int * pint = &i; char c; char * pchar = &c; double d; double * pdouble= &d; printf("%p %p %p\n", pchar, pint, pdouble); /* line 4 */ printf("%p %p %p\n", pchar+1, pint+1, pdouble+1); /* line 5 */ pint++; pchar++; pdouble++; printf("%p %p %p\n", pchar, pint, pdouble); /* line 7 */ pint += 4; pchar += 4; pdouble +=4; printf("%p %p %p\n", pchar, pint, pdouble); /* line 9 */ Suppose the output of the first printf (line 4) is 100 4000 20000 (Recall that printing a pointer directly with %p outputs the content/value of the pointer, which is the address of its pointee, in Hex. To simplify, here we assume it prints in decimal) What is the output of the second printf (line 5)? 101 4004 20008 What is the output of the third printf (line 7)? 101 4004 20008 What is the output of the fourth printf (line 9)? 105 4020 20040

3.5 (2.5 pt) Given the following declaration statements in ANSI C, int arr[]={1,2,3,4,5,6; int * ptr = arr; int i; Specify if each line of (independent) statement below is valid or invalid int * ptr = arr + 3; valid invalid int * ptr = &arr[0] valid invalid i = ptr arr; valid invalid i = ptr + arr; valid invalid i = * (ptr + 2); valid invalid arr++; valid invalid *(arr + 4)=2; valid invalid ptr ++; valid invalid ptr --; valid invalid i = ptr > arr; valid invalid 3.6 (6 pt) Given the following ANSI C program, and assume that the first element of arr is stored in memory address 2000; What is the output of each printf( ) statement? (Recall that %p is used to print the content/value of the pointer, which is the address of its pointee. Here we assume it prints in decimal) main() { int arr[] = {17, 3, 58, 10, 126, 22, 35, 140, 165, 190; int *p = arr; int *q; printf("%p %p %d\n", arr, p, *p); 2000 2000 17 printf("%p %d\n", p+4,*(p+4) ); 2016 126 ++p; printf("%p %d\n", p, *p); 2004 3 p = arr + 5; printf("%p %d\n", p, *(p + 2)); 2020 140 q = p; p = q +3; printf ("%p %p %d %d %d\n", p, q, p-q, *p, *q); 2032 2020 3 165 22 (*p)++; printf("%d %d\n", *(arr+4), *(arr + 8) ); 126 166

Jun 13, 2017 (Tuesday) Name: Student ID: Page 13 of 15 3.7 (1.5 pt) For a function that expects a char array as its argument, its prototype can be either void processarr (char []) or void processarr (char *). True False Assume the following ANSI C code fragment int main(){ char msg[] = "Hello the World"; char * p = msg; processarr ( ); /* line 4 */ void processarr (char []){. Which of the following could be a valid function call to processarr in line 4 a. proceeearr(&msg[2]); b. processarr(p); c. processarr(&p[0]); d. processarr(msg+3); e. All of the above 3.8 (2.5+1 bonus) Consider the following ANSI C code, assume that the first element of msg has starting address 1000; what are the outputs of the three printf statements? Recall that %p prints the content of a pointer variable, and here we assume it prints in decimal. main(){ char msg[] = "HelloWorld"; printf("%s %p %d %d\n", msg, msg, sizeof(msg), strlen(msg)); processarray(msg); HelloWorld 1000 11 10 processarray2(msg); void processarray (char c []){ printf("%s %p %d %d\n", c, c, sizeof(c), strlen(c)); HelloWorld 1000 8 10 void processarray2 (char * c){ printf("%s %p %d %d\n", c, c, sizeof(c), strlen(c)); HelloWorld 1000 8 10

3.9 (1.25pt) Consider the following ANSI C program. int main(){ int a[]={2,4,6, 8,10; int resu = dosthrecursively(a, 5); printf("%d\n", resu); return 0; int dosthrecursively(int* c, int n){ if (n==1) return *c; else return *c + dosthrecursively(c+1, n-1); What is the output of the first printf() in main(), or, explain what does function dosthrecursively intend to do? Sum up first n elements in the array c. 2 +4 +6+8+10=30 3.10 (0.5 pt Bonus) As mentioned in the textbook and class, the declaration of a pointer related variable is intended as a mnemonic (means help you memorize ). For example, declaration int * ptr; can be interpreted as "expression * ptr is an int (and thus ptr is an integer pointer). Based on this rule, guess what the following statements declare. That is, what is the type that ptr is declared to be? You can draw pictures if you like. (We will talk about this next week in class.) int * ptr []; ptr is an array. Each element of the array is a pointer to integer. That is, ptr is an array of (int) pointers ptr int int * ptr [3] (END of TEST)

Jun 13, 2017 (Tuesday) Name: Student ID: Page 15 of 15 Operator precedence (from high to low) and associativity. (You can detach this page, and use it for scratch work )