unit 1 -Introduction to C program part 1
in the string literal indicates that a decimal number (hence the d) should be printed there, with that number being the value of the variable that follows.
%d
How many lines of code will the compiler ignore in the code below?int userAge; int currentDecade; int nextDecade; int nextMilestone; // FIXME: Get user age userAge = 29; // Testing with 29 currentDecade = userAge / 10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10; 1 2 3
3.; The compiler ignores the 2 blank lines and the line that only contains a comment.
The expression wage * 40 * 50 resulted in what value? a.20 b.40000 c.20 * 40 * 50
40000
A compiler generates the following error messages Line 7: Missing semicolonLine 9: numItems not definedLine 1 0: Expected '(' If the programmer does NOT find an error on line 7, the programmer should check line _____. 6 8 9
6
How many spaces will the compiler ignore in the code below? numToBuy = numNeeded - numInStock + 2; 3 6 7
6, before and after the math symbols
A compiler generates the following error messages Line 7: Missing semicolonLine 9: numItems not definedLine 1 0: Expected '(' The programmer should start by examining line _____. 7 9 10
7 start with first given
is text a programmer adds to code, to be read by humans to better understand the code but ignored by the compiler. Two common kinds of comments exist:
a comment
Which instruction completes the program to compute the average of three numbers? x = Get next input y = Get next input z = Get next input_____ Put a to output a. a=(x+y+z)/3 b. a=(x+y+z)/2 c a=x+y+z
a. a. a=(x+y+z)/3
Each statement ends with what symbol? a.Semicolon ; b.Period . c.Colon :
a. semicolon
Indicate the actual output of each statement. Assume userAge is 22. printf("You are %d years.", userAge); a.You are 22 years. b.You are userAge years. c.No output; an error exists.
a.You are 22 years.
indicate the actual output of each statement. Assume userAge is 22. printf("%dyears is good.", userAge); a.22 years is good. b.22years is good. c.No output; an error exists.
b. 22years is good
Program execution begins at main() and executes statements surrounded by which symbols? a.( ) b.{ } c." "
b.{ }
Which instruction completes the program to compute a triangle's area? base = Get next input height = Get next input Assign x with base * height_____ Put x to output a. multiply x by 2 b add 2 to x c. multiply x by 1/2
c multiply x by 1/2
The statement int wage; creates a variable named wage that is used to _____ the value 20. a.input b.output c.hold
c.hold
Given variable numCars = 9, which statement outputs 9? a.printf(numCars); b.printf("numCars"); c.printf("%d", numCars);
c.printf("%d", numCars);
Which statement outputs Hey followed by a new line? a.printf(Hey\n); b.printf("Hey"\n); c.printf("Hey\n");
c.printf("Hey\n");
Which statement outputs: Welcome! a.printf(Welcome!); b.printf "Welcome!"; c.printf("Welcome!");
c.printf("Welcome!");
Each printf() statement outputs items to _____. a.a file named output.txt b.the keyboard c.the screen
c.the screen
A compiler generates the following error messages Line 7: Missing semicolonLine 9: numItems not definedLine 1 0: Expected '(' If the programmer corrects an error on line 7, the programmer should _____. check earlier lines too compile check line 9
compile
Because a syntax error is detected by the compiler, a syntax error is known as a type of
compile time error
Which statement assigns numCars with a number from input? a.scanf("%d", "numCars"); b.scanf("%d" &numCars); c.scanf("%d", numCars); d.scanf("%d", &numCars);
d.scanf("%d", &numCars);
printf(%d, numDogs); Error No error
error " " needed around %d
printf("%d," numDogs); error no error
error " comes before ,
int numCats numCats = 3; printf("%d", numCats); Error No error
error ; needed after int value
printf("%d", NumDogs); Error No error
error NumDogs n is capatlized
printf("Dogs: %d" numDogs); Error No error
error comma needed before numDogs
print("Everyone wins."); Error No error
error is print needs printf
printf("%d", numDogs). Error No error
error is the period
printf("Hello friends!); Error No error
error needs " after !
A compiler warning by default will prevent a program from being created. True False
false
A compiler's default settings cause most warnings to be reported during compilation. True False
false
Spaces are always ignored by the compiler. True False
false
If a compiler generates a specific message like "missing semicolon", then a semicolon must be missing somewhere, though maybe from an earlier line. true false
false The actual error could be different, like missing parentheses. If a programmer makes a mistake, the statement and subsequent statements may still be valid code, but eventually the compiler cannot make sense of the code and generates an error message.
If a compiler says that an error exists on line 90, the actual error may be on line 91, 92, etc. True False
false The actual error may be in an earlier line, but the compiler wasn't able to realize the error immediately, so proceeds to subsequent lines, then gets confused and reports an error.
When a compiler says that an error exists on line 5, that line must have an error. True False
false The error may actually exist in an earlier line, but the compiler didn't get confused until reaching line 5.
A new programmer writes 5 lines of code, compiles and runs, writes 5 more lines, and compiles and runs again. The programmer is _____ . wasting time following good practice
following good practice
A program gets data, perhaps from a file, keyboard, touchscreen, network, etc.
input
/* numKids = 2; /* Typical number */ numCars = 5; */ Valid Invalid
invalid
// Print "Hello" Then print "Goodbye" And finally return. // Valid Invalid
invalid
also called a bug, is an error that occurs while a program runs. For example, a programmer might mean to type numBeans * numJars but accidentally types numBeans + numJars (+ instead of *). The program would compile but would not run as intended.
logic error
starts with /* and ends with * /, where all text between ? * and */ is part of the comment. A multi-line comment is also known as a block comment.
multi line comment
character, \n, in the string literal starts a new output line, called a newline.
newline
printf("Amy // Michael"); Error No error
no error
Would the following order of statements work the same as above? wage = 20; int wage; yes or no
no, int wage; would go first
A program puts that data somewhere, such as to a file, screen, network, etc.
output
construct supports output.
printf
Type a statement that outputs the value of numPeople (an integer variable; do not output a new line), followed by a statement that gets an input value into numPeople.
printf("%d", numPeople); scanf("%d", &numPeople);
Outputting an integer variable's value is achieved via:
printf("%d", x)
A program performs computations on that data, such as adding two values like x + y.
process
An experienced programmer writes 80 lines of code and then compiles and runs. The programmer is probably _____ . programming dangerously following good practice
programming dangerously
The following statement gets an input value and puts the value into variable x:
scanf ("%d", &x) the & before x is necessary to indicate where in memory the read value should be stored
starts with // and includes all the following text on that line. Single-line comments commonly appear after a statement on the same line.
single line comment
Outputting text is achieved via: printf("desired text");. Text in double quotes " " is known as a
string literal
is to violate a programming language's rules on how symbols can be combined to create a program. An example is forgetting to end a statement with a semicolon.
syntax error
Generally, a programmer should not ignore warnings. True False
true
*/ Get user input */ Valid Invalid
valid
/* * Author: Michelangelo * Date: 2014 * Address: 111 Main St, Pacific Ocean */ Valid Invalid
valid
/* numKids = 2; // Typical number numCars = 5; */ Valid Invalid
valid
/* Determine width and height, calculate volume, and return volume squared. */ Valid Invalid
valid
// Get user input Valid Invalid
valid
// Print "Hello" to the screen // Valid Invalid
valid
// numKids = 2; // Typical number Valid Invalid
valid
A compiler will sometimes report a_____ which doesn't stop the compiler from creating an executable program but indicates a possible logic error
warning