Physics 234: Computational Physics

Similar documents
CS242 COMPUTER PROGRAMMING

Computer Programming : C++

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSc Introduction to Computing

Exercise 1.1 Hello world

Lab # 02. Basic Elements of C++ _ Part1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Variables and Constants

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CSCE Practice Midterm. Data Types

CS101 PLEDGED SPRING 2001

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CMSC 202 Midterm Exam 1 Fall 2015

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

C++ for Java Programmers

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS 376b Computer Vision

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Definition Matching (10 Points)

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

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):

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

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

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

a data type is Types

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Name Section: M/W or T/TH. True or False (14 Points)

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

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

LECTURE 02 INTRODUCTION TO C++

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

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

True or False (14 Points)

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

CE221 Programming in C++ Part 1 Introduction

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Chapter 2. Procedural Programming

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Homework #3 CS2255 Fall 2012

Exercise: Using Numbers

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

6.096 Introduction to C++ January (IAP) 2009

Chapter 2: Introduction to C++

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

True or False (15 Points)

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

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

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

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

Do not turn to the next page until the start of the exam.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Final Exam. Name: Student ID: Section: Signature:

CSE 303, Autumn 2006, Final Examination 12 December 2006

REVIEW. The C++ Programming Language. CS 151 Review #2

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

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

2 nd Week Lecture Notes

Input And Output of C++

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

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 3 BASIC INSTRUCTION OF C++

Exercise 3 Logical Operators, Branching, Loops. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

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

Variables. Data Types.

CS 2301 Exam 3 B-Term 2011

Computational Physics Operating systems

CSCE Practice Midterm. Data Types

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CSE 333 Final Exam June 6, 2017 Sample Solution

Introduction to Computer Science Midterm 3 Fall, Points

CSCE 110 PROGRAMMING FUNDAMENTALS

Scientific Computing

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Chapter-8 DATA TYPES. Introduction. Variable:

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

Approximately a Test II CPSC 206

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

Chapter 2: Basic Elements of C++

Lecture 03 Bits, Bytes and Data Types

Programmazione. Prof. Marco Bertini

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

Transcription:

Physics 234: Computational Physics In-class Midterm Exam Friday, February 12, 2010 Student s Name: Fill-in-the-blank and multiple choice questions (20 points) Mark your answers on the exam sheet in blue or black ink. Please be clear about your selections. Each question is worth one point unless otherwise indicated. In code listings where the preamble is not written explicitly, you may assume the following. #include <cassert> #include <iostream> using std::cout; using std::cin; using std::endl; You may also assume that small snippets of code are properly embedded in a main function. 1

1. What output does this produce? signed char a = 128; cout << int(a) << ","; --a; cout << int(a) << endl; (a) 128,127 (b) 128,-127 (C) -128,127 (d) -128,-129 2. What polynomial is computed, and at points on what interval? double horner(double x, const double* a, const double *b) return a < b? *a + x*horner(x,a+1,b) : 0.0; const double p[4] = 1.0, -2.0, 0.25, 0.125 ; int main() for (int n = 0; n <= 100; ++n) const double xn = n/50.0; cout << xn << "\t" << horner(xn,p,p+4) << endl; return 0; (A) 1 2x + 1 4 x2 + 1 8 x3 on the interval [0, 2] (b) 1 2x + 1 4 x2 + 1 8 x3 on the interval [0, 2) (c) x 3 2x 2 + 1 4 x + 1 8 (d) x 3 2x 2 + 1 4 x + 1 8 on the interval [0, 2] on the interval [0, 2) 2

3. Which one of the following in not a synonym for the same fundamental type. Circle your choice. short, short int, int, signed short, signed short int 4. What is the value of N just before the function count_down returns? void count_down(int &N) while (N) N--; int main() count_down(10); return 0; (a) 0 (b) 1 (c) 10 (D) program won t compile 5. Which of the following is the correct scenario? int x, y, z; int main() x = y = z = 0; assert( x == y == z ); return 0; (a) program compiles, runs, and exits cleanly (B) program compiles but fails at runtime on the assert (c) program won t compile 3

6. (2 points) Make note of the various syntax errors by (i) circling the incorrect punctuation and (ii) writing in the missing semicolons. #include " iostream " using std : cout; using std : endl; int main(int argc, char* argv () )...for (int i = 0, i < argc, ++i)...cout >> argv (i) >> endl;...cout >> endl;...return 0; 7. Write this program s output in the box provided. const int N = 4; for (int k = 0; k < N; ++k) for (int j = 0; j < k; ++j) for (int i = 0; i < j; ++i) cout << "(" << i << "," << j << "," << k << ")"; cout << endl; terminal output: (0,1,2)(0,1,3)(0,2,3)(1,2,3) 8. Match each of the following declarations with the most appropriate description. Write the corresponding letter in the space provided. (a) int* p; (b) double x; (c) char a; (d) char r[5]; b c a d high-precision floating point number integer type of exactly one byte in length pointer to a fundamental type 40 contiguous bits of storage 4

9. What are the final values of i and x after the following two statements are executed. (Be sure to express your answers using a literal that is consistent with the type of each variable.) int a = 3, b = -2; const double x = ++a+-b*5+9 / 5+a; a == 4 x == 19.0 10. Suppose that your computer represents signed char internally using the two s complement scheme. Carry out the addition a+b by hand and fill in the empty boxes. signed char a = 10; signed char b = -30; signed char c = a+b; Hint: decimal two s complement 9 (00001001) 2 23 (11101001) 2 29 (11100011) 2 0 0 0 0 1 0 1 0 + 1 1 1 0 0 0 1 0 1 1 1 0 1 1 0 0 11. (2 points) Given the C array definitions, char p[] = p, h, y, s, 2, 3, 4, \0 ; char q[] = "phys123"; what do each of the following equalities evaluate to? In each case, circle either true (t), false (f), or unpredictable (u). (t / f / U) p < q; (t / F / u) *p < *q; (T / f / u) *(p+6)!= *(q+6); (T / f / u) *(p+7) == *(q+7); 5

12. This program computes the matrix product C = AB. Matrix elements are stored in flat arrays arranged in row-major order. What s the correct body of the index function? int index(int i, int j, int M)... void mult(double out[], const double inl[], const double inr[], int N, int K, int M) for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) double sum = 0.0; for (int k = 0; k < K; ++k) sum += inl[index(i,k,k)]*inr[index(k,j,m)]; out[index(i,j,m)] = sum; int main() const double A[3*4] = 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ; const double B[4*2] = -3.0, 0.1, -4.0, 0.25, 1.5, 3.6 2.25, 0.0 ; double C[3*2]; mult(c,a,b,3,4,2); return 0; (A) return i*m+j; (b) return i+j*m; (c) return (i-1)*m+j-1; (d) return i*j+m; 6

13. Suppose I invoke a program by typing this at the command line: $./myprog - - verbose -n 5 file1.dat file2.dat Circle the two characters that are accessbile as argv[1][1] and argv[3][0] from within main(int argc, char* argv[]). 14. What is the best mathematical interpretation of the calculation carried out by the function call bf(-3.0,1.5,6)? double bf(double E, double kt, long unsigned int n) const double x = -E/kT; E = 1.0; while (n > 0) (E *= x/n--) += 1; return E; (a) 2 1 + 22 + 23 + 24 + 25 + 26 2! 3! 4! 5! 6! (b) 1 + 2 + 4 + 8 + 16 + 32 2! 3! 4! 5! (c) 1 2 + 4 8 + 16 32 + 64 2! 3! 4! 5! 6! (D) 1 + 2 + 2 + 4 3 + 2 3 + 4 15 + 4 45 15. This program builds a histogram with 100 bins equally-spaced between a and b. What s the correct definition of the bin index i? unsigned long int hist[100], n = 0; const double a = 1.0, b = 12.5; double d; while(cin >> d) // read in fp values from stdin const int i =... ; if (i < 100) ++hist[i]; ++n; (a) d/100/(a+b); (b) d*100/(b-a); (C) (d-a)/(b-a)*100; (d) d/100; 7

16. (3 points) Suppose we have defined a C string const char config[] = "+-++++--++--++-++---+++++---"; that encodes the spin-up or spin-down character of a linear chain of localized electrons. We want to compose a function named ferro that counts the number of neighbouring electrons that are similarly directed. The function we come up with is the following. int ferro(const char* const p) int num = 0; while (*++p!= \0 ) num += *(p-1) == *p; return num; Unfortunately, this first attempt isn t quite right. (i) Cross out the one const modifier that prevents this program from working correctly. (ii) Will the statement ferro(config); still execute if we remove both const modifiers? (yes / NO) (iii) Will the program work identically if while(*++p!= \0 ) is replaced by while(*++p)? (YES / no) 8