CS 0449 Sample Midterm

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

Arrays and Pointers in C. Alan L. Cox

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Pointers, Arrays, and Strings. CS449 Spring 2016

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Common Misunderstandings from Exam 1 Material

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

High Performance Programming Programming in C part 1

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Fundamental of Programming (C)

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Understanding Pointers

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

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

Array Initialization

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Midterm Exam Review Slides

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 61c: Great Ideas in Computer Architecture

Lecture 04 Introduction to pointers

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Procedures, Parameters, Values and Variables. Steven R. Bagley

CSC209H1S Day Midterm Solutions Winter 2010

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSE 333 Lecture 2 Memory

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

1d: tests knowing about bitwise fields and union/struct differences.

Programming in C - Part 2

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

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

Variation of Pointers

Strings and Stream I/O

SOFTWARE Ph.D. Qualifying Exam Fall 2017

CS 1713 Introduction to Programming II

A brief introduction to C programming for Java programmers

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

ECE264 Fall 2013 Exam 1, September 24, 2013

COMP 2355 Introduction to Systems Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Midterm Exam 2 Solutions, C programming

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CS 223: Data Structures and Programming Techniques. Exam 2

CSE 333 Lecture 7 - final C details

PROGRAMMAZIONE I A.A. 2017/2018

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Final Intro to C Review

4) In C, if you pass an array as an argument to a function, what actually gets passed?

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Pointers. Pointer References

Arrays and Memory Management

CS 241 Data Organization Pointers and Arrays

Final CSE 131B Spring 2004

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

A Short Course for REU Students Summer Instructor: Ben Ransford

ELEC 377 C Programming Tutorial. ELEC Operating Systems

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

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

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

CS61, Fall 2012 Section 2 Notes

Chapter 13. Functions and Parameter Passing (Part 2)

Pointers. Pointers. Pointers (cont) CS 217

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

2. Which of the following will print the value 2 for the above code?

ECE551 Midterm Version 1

ME964 High Performance Computing for Engineering Applications

Question 1. [15 marks]

CSE 333 Autumn 2013 Midterm

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

ECE Fall 20l2, Second Exam

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

PRINCIPLES OF OPERATING SYSTEMS

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Procedural programming with C

CS 1713 Introduction to Programming II

Work relative to other classes

Memory Allocation in C

Pointers, Dynamic Data, and Reference Types

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

UNDEFINED BEHAVIOR IS AWESOME

EL2310 Scientific Programming

Introduction to C. Systems Programming Concepts

Transcription:

Name: CS 0449 Sample Midterm Multiple Choice 1.) Given char *a = Hello ; char *b = World;, which of the following would result in an error? A) strlen(a) B) strcpy(a, b) C) strcmp(a, b) D) strstr(a, b) 2.) Given int *p;, which of the following is an invalid operation? A) p * 2 B) &p C) *p D) p++ 3.) Suppose you are reading a record from a file into a struct. One of the fields of that record may contain an integer or a string depending on the situation. What would be the best type for the member variable that holds the field that minimizes memory usage? A) struct B) array C) char* D) union 4.) Given variable declaration const char *p;, which of the following is a legal statement? 1

A) &p = NULL; B) *p = 0; C) p = NULL; D) None of the above 5.) Which of the following is false about storage classes? A) A static global variable is visible across the entire program B) An automatic local variable is deallocated at the end of its scope C) A global variable is initialized when the program starts D) There is no storage class with an automatic lifetime that is visible across the entire program Short Answer 6.) Your program crashed due to an invalid pointer access. But strangely you don t see output from a previous printf() call that should have executed before the pointer access. What might be the reason? 7.) Given unsigned int c;, write the correct statement using bitwise operators to do the following. a.) Set the 15 th bit of c to 1 b.) Set the 15 th bit of c to 0 2

8.) Explain two distinct reasons why you would pass a pointer to a variable to a function, instead of the variable itself. 9.) For each of the below code snippets, 1) State what is wrong with the program and 2) State exactly how you would fix it by referencing the line numbers provided. a.) The below program should print Two. 1. void update(char *s) { s = "Two"; 2. int main() 3. { 4. char *str = "One"; 5. update(str); 6. printf("%s\n", str); 7. return 0; 8. 3

b.) The below function format(...) should return a string formatted with the given number. 1. char* format(int n) 2. { 3. char str[100]; 4. sprintf(str, %d\n, n); 5. return str; 6. 10.) Given the declaration char a[2][3] = {{10, 20, 30, {40, 50, 60; and the layout in memory given below, write the numerical value of the following expressions. If you believe the expression is illegal, write illegal and explain why. Address Memory Name of Storage Location 0x200 10 a[0][0] 0x204 20 a[0][1] 0x208 30 a[0][2] 0x20c 40 a[1][0] 0x210 50 a[1][1] 0x214 60 a[1][2] a.) What is the value of a? b.) What is the value of a[0]? c.) What is the value of a+1? d.) What is the value of a[0]+1? e.) What is the value of *a[0]+1? (Hint: * operator has higher precedence compared to + operator.) 4

f.) What is the value of *(a[0]+1)? Tracing 11.) What is the output when the following program is run? Assume sizeof(char) == 1, sizeof(long long) == 8, and sizeof(char *) == 8. #include <stdio.h> struct { char x; long long y; a; union { char x; long long y; b; char buf[100]; int main() { char *p = buf; printf("sizeof(a)=%d\n", sizeof(a)); printf("sizeof(b)=%d\n", sizeof(b)); printf("sizeof(buf)=%d\n", sizeof(buf)); printf("sizeof(p)=%d\n", sizeof(p)); return 0; 5

12.) If the program below, what would be the output? #include <stdio.h> void even(int n) { printf("%d even\n", n); void odd(int n) { printf("%d odd\n", n); void (*f[4])(int n); int main() { int i; void (*foo)(int n); for(i = 0; i < 4; ++i) { if(i % 2) f[i] = odd; else f[i] = even; for(i = 0; i < 4; ++i) { foo = f[i]; foo(i); return 0; 6

Coding 13.) Write a program that asks the user to input a word and then reverses the characters in the word. Match the example output. You can assume the word is less than 100 characters. For example: Enter a word: hello Reverse of hello is olleh or Enter a word: world Reverse of world is dlrow 7

14.) Write a number search program that takes two command line arguments: 1) length of the array to generate and 2) the number to search within the array. The program should define an array of 100 integers and automatically fill it with the powers of 2 (1, 2, 4, 8, 16 ) up to the given length and the rest with 0s. You can assume that the given length is less than 100. This is an example output assuming the name of the program is search :./search 10 32 Number 32 found in index 5../search 10 50 Number 50 was not found. Part of the program, specifically the search function, has already been implemented for you. Your job is to IMPLEMENT THE MAIN FUNCTION using the given search function as described in the comment. Check that there are exactly two command line arguments and if not, print an error message and return. (Hint: You are free to use the int atoi(const char *nptr) function defined in stdlib.h to convert argument strings to integers.) #include <stdio.h> #include <stdlib.h> // [Arguments] // begin: pointer to the beginning of array // end: pointer to the end of array // needle: value that is being searched for // [Return Value] // If not found: NULL // If found: pointer to element of array containing needle value int* search(int* begin, int* end, int needle) { int *p; for(p = begin; p <= end; p++) { if(*p == needle) return p; return NULL; // IMPLEMENT THIS int main(int argc, char **argv) { int nums[100] = {0; // 1. Check and convert command line arguments using atoi. // 2. Fill array nums with powers of 2 up to length numbers. // 3. Use above search( ) function to search array with needle number. // 4. Print search result using the value returned by search( ). return 0; 8