CMPSC 16 PRACTICE MIDTERM 2 SUMMER All programming questions in this exam should be answered based on the programming language C.

Similar documents
Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

arrays C Programming Language - Arrays

Illustration 1: Diagram of program function and data flow

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

The C Programming Language course syllabus associate level

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

J a v a Quiz (Unit 3, Test 0 Practice)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Stacks. Linear data structures

An Incomplete C++ Primer. University of Wyoming MA 5310

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

C++FA 5.1 PRACTICE MID-TERM EXAM

Lecture 22: C Programming 4 Embedded Systems

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

CS 241 Data Organization Coding Standards

Sources: On the Web: Slides will be available on:

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Chapter 13 Storage classes

AP Computer Science Java Mr. Clausen Program 9A, 9B

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

13 Classes & Objects with Constructors/Destructors

Change Impact Analysis

Java Interview Questions and Answers

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Introduction to Java

CSE 8B Midterm Fall 2015

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Passing 1D arrays to functions.

Conditionals (with solutions)

FEEG Applied Programming 5 - Tutorial Session

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

Moving from CS 61A Scheme to CS 61B Java

Data Structures using OOP C++ Lecture 1

Sample CSE8A midterm Multiple Choice (circle one)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Implementation Aspects of OO-Languages

5 Arrays and Pointers

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

In this Chapter you ll learn:

Habanero Extreme Scale Software Research Project

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CUDA Programming. Week 4. Shared memory and register

Load Balancing. computing a file with grayscales. granularity considerations static work load assignment with MPI

Introduction to Java. CS 3: Computer Programming in Java

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

The C Programming Language

Performance Improvement In Java Application

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Arrays in Java. Working with Arrays

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Object Oriented Software Design II

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

C Compiler Targeting the Java Virtual Machine

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

System Calls and Standard I/O

C Interview Questions

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

/* File: blkcopy.c. size_t n

Data Structures and Algorithms Written Examination

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

CS 2412 Data Structures. Chapter 2 Stacks and recursion

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Pseudo code Tutorial and Exercises Teacher s Version

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Example of a Java program

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Stack Allocation. Run-Time Data Structures. Static Structures

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

System Calls Related to File Manipulation

Introduction to Programming System Design. CSCI 455x (4 Units)

Lab Experience 17. Programming Language Translation

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

C++ INTERVIEW QUESTIONS

1 Abstract Data Types Information Hiding

Glossary of Object Oriented Terms

8.1. Example: Visualizing Data

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Transcription:

CMPSC 16 PRACTICE MIDTERM 2 SUMMER 2014 All programming questions in this exam should be answered based on the programming language C.

Sample Exam Problem. Know the definitions of the following terms and be able to match them with a desciptive phrase (as in exam 1). abtraction, reusability, function invocation, function definition, function prototype, formal parameters, actual prameters, call-by-value, scope, global variable, local variable, recursive function, array, dereference operator, address operator, pointer, dynamic memory, static memory. Sample Exam Problem. Consider an input file input.txt with the following content: CS 16 8/5.2014. The first character in the file is C and the last character is.. There is a blank (space) character after "CS". Consider the code segment below. Show the contents of the file output.txt after this code is executed by completing the output shown below: CODE SEGMENT: #include <stdio.h> int main(void) { char c; int value = 0; int i; FILE * input, * output; input = fopen("input.txt", "r"); output = fopen("output.txt", "w"); fscanf(input, "%c", &c); fprintf(output, "c: %c\n\n", c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value); fscanf(input, "%c", &c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value); OUTPUT: c: value: value: c: value: for(i = 0; i <= 6; i++) fscanf(input, "%c", &c); fprintf(output, "c: %c\n\n", c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value);

Sample Exam Problem. Show the contents of the array x after all the statements in the following code segment are executed. List the contents of x starting with the first element of the array, and write a question mark? if the contents are not defined. CODE SEGMENT: CONTENTS OF x: int x[5]; x[2] = 1; x[3] = 5; x[x[2]] = x[2] * 2; x[5 - x[1]] = 9; /* What are the contents of x here? */ Sample Exam Problem. Write a C programming statement using malloc that will declare a pointer named xp which points to a dynamically allocate block of memory with space for 10 integer values. Fill in your answer on the blank line. (Hint: see the lab on pointers). int * xp =

Sample Exam Problem. Given the following declarations, show the results of evaluating the following expressions. If the expression accesses a section of memory that contains unknown values write?. (Hint: for studying, try it out in ch) int y[6] = { 2,3,5,7,11,13; int *yp = &y[2]; EXPRESSION: *y VALUE: *yp *(yp + y[0]) *yp + y[0] yp[4] + 1

Sample Exam Problem. Given the following code segment, show the output that will be generated by this code by completing the partial output shown below. CODE SEGMENT: #include<stdio.h> int value = 0; void func1(int x) { x = 5; void func2(int *x) { *x = 10; void func3(int x) { value = x; int main(void) { int x = 0; func1(x); printf("x is: %d\n\n", x); func2(&x); printf("x is: %d\n\n", x); OUTPUT: x is: x is: value is: value is: x is: value is: value is: func1(value); printf("value is: %d\n\n", value); func2(&value); printf("value is: %d\n\n", value); func3(5); printf("x is: %d\n\n", x); printf("value is: %d\n\n", value); func2(&value); func3(value - 1); printf("value is: %d\n\n", value);

Sample Exam Problem Write a code segement to compute the sum of three 3-by-3 matrices. We want to compute D = A + B + C. Fill in the missing portion below. #include<stdio.h> int main(){ int A[3][3] = {{-1,3,6,{19, -8-1,{-8, -6, 1; int B[3][3] = {{12, 0, 0, {5, 2, -1, {6,5,7; int C[3][3] = {{9,8,7, {1,2,3, {5,4,6; int D[3][3]; int i, j, n = 3; // Compute D = A + B + C // Fill in the missing code here // print D printf("\nmatrix D = A + B + C:\n"); for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ printf("%3i ", D[i][j]); printf("\n"); The program should produce the following expected output: Matrix D = A + B + C: 20 11 13 25-5 2 3 3 14

Sample Exam Problem. Consider the imaginary memory layout below in which addresses are four digit integers and all values stored in memory are represented as positive integers. Assume that an integer takes up 4 bytes, so that adjacent memory addresses differ by 4. So, incrementing an address by 1 results in an address that is 4 bytes away. For example, &y is 1012, &y + 1 is 1016, &y + 2 is 1020, and so on. The addresses, memory contents, and variable names associated with particular memory blocks are shown in the table. Address 1000 1004 1008 1012 1016 1020 1024 1028 Memory Contents 4 1024 10 5 1004 16 1012 1 Variable x y z Fill in the following table showing the value of the given expressions. EXPRESSION: x VALUE: *1008 &y *&y &x+1 *z+4 **(&x+4) *(&z+1)

Sample Exam Problem. Write a function that searches for a value in an array. The function will take three arguments: 1) an integer value that is greater than 0 and indicates the size of the array, 2) an integer array of that size, and 3) an integer value that we would like to search. The function should return an integer value. It should return -1 if the value that is being searched is not in the array. Otherwise, it should return the position of the value in the array (return 0 for the first position). Assume that there are no duplicates in the list - every item is unique. #include <stdio.h> int findval(int size, int values[], int value); int main(void) { int arraysize =...; int result; int value =...; int myarray[] =...; result = findval(arraysize, myarray, value); if (result == -1) printf("the value %d is not in the list.\n", value); else printf("the value %d is at position %d in the list.\n", value, result); int findval(int size, int values[], int value) { int result, i; /* write the missing part below */ return result;

Sample Exam Problem. Write a recursive function that adds all the elements in an array. The function will take two arguments, 1) an integer value that is greater than 0 and indicates the size of the array, and 2) an integer array of that size. The function should return an integer value that is the sum of the elements in the array. The function should add elements of the input array using recursion and should not use a loop. #include <stdio.h> int recursivesum(int size, int values[]); int main(void) { int arraysize =...; int result; int myarray[] =...; result = recursivesum(arraysize, myarray); printf("the sum is %d\n", result); int recursivesum(int size, int values[]) { /* write the missing part below */