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

Similar documents
The University of Nottingham

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

The University of Nottingham

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Kurt Schmidt. October 30, 2018

Pointers, Dynamic Data, and Reference Types

Common Misunderstandings from Exam 1 Material

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS 0449 Sample Midterm

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

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

Arrays and Pointers in C. Alan L. Cox

Advanced Systems Programming

Fundamental of Programming (C)

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 61c: Great Ideas in Computer Architecture

PRINCIPLES OF OPERATING SYSTEMS

Introduce C# as Object Oriented programming language. Explain, tokens,

speller.c dictionary contains valid words, one per line 1. calls load on the dictionary file

Tokens, Expressions and Control Structures

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

A brief introduction to C programming for Java programmers

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

Arrays (1A) Young Won Lim 1/27/17

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Operators. The Arrow Operator. The sizeof Operator

Fast Introduction to Object Oriented Programming and C++

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Low-Level C Programming. Memory map Pointers Arrays Structures

Pointers (continued), arrays and strings

CS 61C: Great Ideas in Computer Architecture Introduction to C

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Dynamic Data Structures. CSCI 112: Programming in C

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Data Representation and Storage

Lectures 5-6: Introduction to C

CS61C Midterm Review on C & Memory Management

Heap Arrays and Linked Lists. Steven R. Bagley

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 12

CSE 431S Type Checking. Washington University Spring 2013

CS31 Discussion 1E Spring 17 : week 08

Pointers (continued), arrays and strings

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Heap Arrays. Steven R. Bagley

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

G52CPP C++ Programming Lecture 18

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

Programming in C - Part 2

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

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

Basic Types, Variables, Literals, Constants

C programming basics T3-1 -

CSCI 171 Chapter Outlines

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Memory, Arrays & Pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

Lectures 5-6: Introduction to C

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS201- Introduction to Programming Current Quizzes

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

Introduction to C++ with content from

Type Conversion. and. Statements

G52CPP C++ Programming Lecture 9

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

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

OBJECT ORIENTED PROGRAMMING

Questions: ECE551 PRACTICE Final

Variables. Data Types.

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Pointers and File Handling

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

COMP 2355 Introduction to Systems Programming

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Lecture 2, September 4

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Pointers in C/C++ 1 Memory Addresses 2

Arrays and Memory Management

Intermediate Programming, Spring 2017*

The C++ Object Lifecycle. EECS 211 Winter 2019

template <typename T> // unless it s an unqualified pointer struct IsPtr<T *> { enum { r = true }; };

ESc101: Pointers and Arrays

CS24 Week 2 Lecture 1

G52CPP C++ Programming Lecture 13

05-01 Discussion Notes

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Introduction to C. Systems Programming Concepts

Transcription:

Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true 1b only C++ since it has a function in a struct i.e. (ii) only 1c: tests knowing FILE* and the basic FILE* functions. Correct code: FILE *file; file = fopen( argv[1], "w" ); fputs( argv[2], file ); fclose(file); So errors are on the file. lines, and with the missing * 1d: tests knowing about bitwise fields and union/struct differences. sizeof(a) = 4 = sizeof(int), since sizeof(int) > sizeof(char) sizeof(b) = 5 = sizeof(int) + sizeof(char) sizeof(c) = 5 = sizeof(a) + sizeof(char) sizeof(d) = 5 = sizeof(c) > sizeof(a) sizeof(e) = 1; bit fields sizeof(f) = 1 = max of sizeof(e) or 1. If their answer for E was bigger than 1 then accept the bigger answer. 1e tests whether they understand the pointer arithmetic i) Problem is that the zero terminator on first string gets copied. ii) Will just output the first string, i.e. Hello iii) Add the text p--; just after or just before the text q = str2;, to remove the splitting \0. Also accept any equivalent answer to remove the 0, presumably by changing p. 1f Mainly tests knowing. vs -> i) text should be x->a.i == y.a.i i.e. Left of == is: x->a.i right of == is y.a.i

ii) Should be comparei( &b1, b2 ) Question 2 2a struct _LE * (Since it is C not C++, they need the struct in there.) 2b c not c++ so has to use malloc. Needs the +1 for NULL terminator pentry->pfirstname = malloc( strlen(pfirstname)+1 ); strcpy(pentry->pfirstname, pfirstname ); pentry->plastname = malloc( strlen(plastname)+1 ); strcpy(pentry->plastname, plastname ); 2c i) (Insert at head of list) pnewentry->pnext = g_phead; g_phead = pnewentry; ii) (Insert elsewhere in list) pnewentry->pnext = ppreviousentry->pnext; ppreviousentry->pnext = pnewentry; iii) (Iteration through list) (ppreviousentry->pnext!= NULL) && ppreviousentry->pnext->iage > iage Order is important. Must check NULL prior to dereferencing. Note: very similar to the condition for inserting at head of list. 2d The simple solution: bool operator==( const ListEntry& o1, const ListEntry& o2 ) return strcmp( o1.plastname, o2.plastname ) == 0; (End of simple solution) Alternatively, they could make it a member of ListEntry struct if they want. If so, then it needs to at least be declared (even if not defined) within struct declaration, and won t be able to use the name ListEntry. e.g. typedef struct _LE char* pfirstname;

char* plastname; int iage; struct _LE* pnext; bool operator==( const _LE& o2 ) const return strcmp( this->plastname, o2.plastname ) == 0; ListEntry; Also note: They don t have to use strcmp if they can find an equivalent way. They could use!strcmp() instead of strcmp()==0 For full marks, const should be specified for both parameters (including implicit this pointer (i.e. const function) if the member function version is used). Question 3 3a: 2 other methods created implicitly Copy constructor Assignment operator 3b 4,7,48,0,2,2,17 3c (only first number is actual answer, rest is explanation) 1 initial (i=2) 2 (i=3, i=10) 11 (i=10 still) 11 (i=10 still, then I = 20) 20 static = 21 21 static = 22, I = 30 30 pi moves on. 1 = diff in positions. 3d (i) i.e. elipsis/3 dots. (ii) 1 : Caught an A 2 : Caught an A 3 : Caught something else 4 : Caught a B*

Question 4 4a Multiple functions with the same name but different parameter types, where the parameter types are used to determine which function to call. 4b Tests knowing difference between struct and class Sample 1 won t compile because the constructor is private (by default for class) Sample 2 will compile. 4c Tests not being scared of function pointers Note: func a means return 2, func b means return input + 1 i = 2 j = 21 k = 2 l = 2 m = 3 n = 62 f1 = a, f2=a f1 = b, f2=a So answer: 2,21,2,2,3,62 4d Copy constructor, assignment operator 1 (a=1,e=1,f=1) 4 (b=4) 3 (c=a+2=3) 6 (d=b+2=6) 9 (e=b+5) 11 (f=b+5=a+5+5) 4e Dynamic cast is used to safely cast from a base class pointer or reference to a sub-class pointer or reference. It will verify that the object really is of the sub-class type and if not it will return NULL (if casting a pointer) or throw an exception (if casting a reference). Example: class A ; class B : A void foo( A* pa )

B* pb = dynamic_cast<b*>(pa); if ( pb == NULL ) // Handle cannot cast Question 5 5a) Only (iv) is valid. Note: (iii) has a semi-colon at the end. (i) and (ii) use some kind of return. i.e. #define DIFF(x,y) ((x)>(y))? ((x)-(y)) : ((y)-(x)) 5b) int diff( int x, int y ) return (x>y)? (x-y) : (y-x); 5c) template <class T1, class T2> T1 diff( T1 x, T2 y ) return (T1)((x>y)? (x-y) : (y-x)); For full marks: should say template <class > or <typename > Needs two parameter types, with different names Return type should match param 1 type Return type should be cast to type of param 1 5d) Tests knowing non-virtual funcs are different from Java. 15 Initial: b = 15, s = (base 25, sub 20) 20 Then b = 1 1 Then s = (base 2, sub 20) 20 2 20 Then s = (base 3, sub 20) 3 Then s = (base 3, sub 20) 20

5e) Incorrect lines are all due to const. F G O Q S