Homework #3 CS2255 Fall 2012

Similar documents
FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CS2255 HOMEWORK #1 Fall 2012

Exam 3 Chapters 7 & 9

CSC 1300 Exam 4 Comprehensive-ish and Structs

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

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Pointers. Variable Declaration. Chapter 10

Introduction to Computer Science Midterm 3 Fall, Points

UNIT- 3 Introduction to C++

What is an algorithm?

[0569] p 0318 garbage

pointers + memory double x; string a; int x; main overhead int y; main overhead

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

CS201- Introduction to Programming Current Quizzes

Add Subtract Multiply Divide

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Lecture 3 Tao Wang 1

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

CS 161 Exam II Winter 2018 FORM 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Pointers II. Class 31

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Pointer Data Type and Pointer Variables

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Algorithms & Data Structures

Pointers. Memory. void foo() { }//return

C++ for Java Programmers

What have we learned about when we learned about function parameters? 1-1

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Chapter 11: Pointers

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

BITG 1233: Introduction to C++

Pointers! Arizona State University 1

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Reference operator (&)

Objectives. In this chapter, you will:

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Dynamic Memory Allocation

CS2141 Software Development using C/C++ C++ Basics

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

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

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

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

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015


KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Chapter 2: Introduction to C++

VARIABLES & ASSIGNMENTS

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

A First Program - Greeting.cpp

Pointers, Arrays and C-Strings

CS201 Latest Solved MCQs

Pointers, Dynamic Data, and Reference Types

Chapter 9. Pointers and Dynamic Arrays

CS61C : Machine Structures

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers


Computer Programming : C++

Variation of Pointers

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 2: Using Data

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Pointers. Reference operator (&) ted = &andy;

CSI33 Data Structures

CHAPTER 3 BASIC INSTRUCTION OF C++

CS 61c: Great Ideas in Computer Architecture

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Arrays, Pointers and Memory Management

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Programming in C++ 5. Integral data types

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

The C++ Language. Arizona State University 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

CS201 Some Important Definitions

CSC 211 Intermediate Programming. Pointers

Transcription:

Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation point (! ) 2. With pointer variables, you can manipulate data stored in other variables. a. never b. seldom c. indirectly Provide a three-line (or less) C++ statement which emulates your answer for question #2 3. The statement int *ptr; has the same meaning as a. int ptr; b. *int ptr; c. int ptr*; d. int* ptr; 4. When you work with a dereferenced pointer, you are actually working with: a. a variable whose memory has been deallocated b. a copy of the value pointed to by the pointer variable c. the actual value of the variable whose address is stored in the pointer variable Provide a three-line (or less) C++ statement which emulates your answer for question #4. Start with: int x = 3; 5. These can be used as pointers. a. Array names b. Numeric constants c. Punctuation marks 6. The contents of pointer variables may be changed with mathematical statements that perform: a. all mathematical operations that are legal in C++ b. multiplication and division c. addition and subtraction d. b and c 7. A pointer may be initialized with a. the address of an existing object b. the value of an integer variable c. the value of a floating point variable d. all of these

8. What does the following statement do? double *num2; a. Declares a double variable named num2. b. Declares and initializes an pointer variable named num2. c. Initializes a variable named *num2. d. Declares a pointer variable named num2. 9. (EXTRA CREDIT) When the less than ( < ) operator is used between two pointer variables, the expression is testing whether a. the value pointed to by the first is less than the value pointed to by the second b. the value pointed to by the first is greater than the value pointed to by the second c. the address of the first variable comes before the address of the second variable in the computer's memory d. the first variable was declared before the second variable 10. (EXTRA CREDIT) Look at the following statement: sum += *array++; This statement... a. is illegal in C++ b. will always result in a compiler error c. assigns the dereferenced pointer's value, then increments the pointer's address d. increments the dereferenced pointer's value by one, then assigns that value 11. Use the delete operator only on pointers that were a. never used b. not correctly initialized c. created with the new operator d. dereferenced inappropriately 12. A function may return a pointer, but the programmer must ensure that the pointer. a. still points to a valid object after the function ends b. has not been assigned an address c. was received as a parameter by the function d. has not previously been returned by another function 13. Which of the following statements is not valid C++ code (assume num1 was declared as a float)? a. int ptr = &num1; b. int ptr = int *num1; c. float num1 = &ptr2; are valid e. All of these are invalid 14. A pointer with the value 0 (zero) is called a NULL pointer. 15. When this is placed in front of a variable name, it returns the address of that variable. a. asterisk ( * ) b. conditional operator c. ampersand ( & ) d. semicolon ( ; )

16. What will the following statement output? Int num1 = 3; cout << &num1; a. The value stored in the variable called num1. b. The memory address of the variable called num1. c. The number 1. d. The string "&num1". 17. A pointer variable is designed to store a. any legal C++ value. b. only floating-point values. c. a memory address. d. an integer. 18. Look at the following statement. int *ptr; In this statement, what does the word int mean? a. the variable named *ptr will store an integer value b. the variable named *ptr will store an asterisk and an integer value c. ptr is a pointer variable that will store the address of an integer variable 19. Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr; a. the value stored in the variable whose address is contained in ptr. b. the string "*ptr". c. the address of the variable stored in ptr. d. the address of the variable whose address is stored in ptr. 20. The and operators can be used to increment or decrement a pointer variable. a. addition, subtraction b. modulus, division c. ++, -- 21. Not all arithmetic operations may be performed on pointers. For example, you cannot or a pointer. a. multiply, divide b. add, subtract c. +=, -= d. increment, decrement 22. Which statement displays the address of the variable num1? a. cout << num1; b. cout << *num1; c. cin >> &num1; d. cout << &num1;

23. The statement cin >> *num3; a. stores the keyboard input into the variable num3. b. stores the keyboard input into the pointer called num3. c. stores the keyboard input into the variable pointed to by num3. Provide an example declaration for the variable num3 prior to the execution of the statement. 24. Dynamic memory allocation occurs a. when a new variable is created by the compiler b. when a new variable is created at runtime c. when a pointer fails to dereference the right variable d. when a pointer is assigned an incorrect address 25. The statement int *ptr = new int; a. results in a compiler error. b. assigns an integer less than 32767 to the variable named ptr. c. assigns an address to the variable named ptr. d. creates a new pointer named int. 26. When using the new operator with an older compiler, it is good practice to: a. test the pointer for the NULL address b. use a preprocessor directive c. clear the data from the old operator 27. Every byte in the computer's memory is assigned a unique a. pointer b. address c. dynamic allocation d. name 28. It is legal to subtract a pointer variable from another pointer variable. Justify your answer 29. A pointer variable may be initialized with a. any non-zero integer value within the integer range. b. any address in the computer's memory allowed by the Operating System. c. an address less than 0 d. a and c only. 30. If a variable uses more than one byte of memory, for pointer purposes its address is: a. the address of the last byte of storage. b. the average of the addresses used to store the variable. c. the address of the first byte of storage. Explain how this relates to an array of integers 31. What will the following code output? int number = 22; int *var = &number; cout << *var << endl; a. The address of the number variable c. An asterisk followed by 22 b. 22 d. An asterisk followed by the address of the number variable

32. What will the following code output? int number = 22; int *var = &number; cout << var << endl; a. The address of the number variable c. An asterisk followed by 22 b. 22 d. An asterisk followed by the address of the number variable 33. What will the following code output? int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl; a. Five memory addresses d. 2 b. 0 e. 1 c. 3 34. Look at the following code. int numbers[] = {0, 1, 2, 3, 4 }; int *ptr = numbers; ptr++; After this code executes, which of the following statements is true? a. ptr will hold the address of numbers[0] b. ptr will hold the address of the 2nd byte within the element numbers[0] c. ptr will hold the address of numbers[1] d. This code will not compile. 35. An array name is a pointer constant because the address stored in it cannot be changed during runtime. 36. C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array. 37. A pointer can be used as a function argument, giving the function access to the original argument. Explain what this means in terms of scope in terms of the calling function (which could be main) and/or the function itself. 38. The ampersand (&) is used to dereference a pointer variable in C++. 39. Assuming myvalues is an array of int values, and index is an int variable, both of the following statements do the same thing.

cout << myvalues[index] << endl; cout << *(myvalues + index) << endl;