Introduction to computer programming, variables, and comments Module 1 Test Answers Full 100%
-
The feature of a high-level language which makes it possible to use that language on many different computers is called:
- portability
- availability
- compatibility
Explanation & Hints: The feature of a high-level programming language that makes it possible to use the language on many different computers is called portability. Portability refers to the ability of software to be run on various hardware platforms with little or no modification. This is a key feature of high-level languages, as they abstract away hardware-specific details, allowing programs written in one environment to be executed in another relatively easily.
-
A file which contains a computer program translated into machine language is called:
- an executable file
- a source file
- an interchange file
-
Explanation & Hints: The correct answer is portability.
Portability in the context of high-level programming languages refers to the ability of software to run on various hardware platforms without needing significant modifications. This is possible because high-level languages abstract away many of the hardware-specific details. For example, a program written in a language like Java or Python can be run on different operating systems (Windows, macOS, Linux) without changing the code. The language’s compiler or interpreter takes care of adapting the program to the specific hardware and operating system. This makes software development more efficient and allows developers to reach a wider audience with the same application.
-
Select all the true statements:
- The “C” language compiler is case-sensitive
- The reserved keywords cannot be used as variable names
- You can write multiline comments in the “C” language
- The decimal value of
012
(octal) is10
- Nesting comments is a recommended pratice
- The decimal value of
0x11
(hexadecimal) is16
-
Explanation & Hints: Here are the true statements from the options provided:
- The “C” language compiler is case-sensitive – This is true. In C, identifiers (including variable names, function names, etc.) are case-sensitive, meaning that
Variable
,variable
, andVARIABLE
are considered different identifiers. - The reserved keywords cannot be used as variable names – This is true. In C, reserved keywords, such as
int
,return
, andfor
, are part of the language syntax and cannot be used as variable names because they have predefined meanings in the language. - You can write multiline comments in the “C” language – This is true. In C, multiline comments start with
/*
and end with*/
, allowing comments to span multiple lines. - The decimal value of 012 (octal) is 10 – This is true. In C, numbers starting with a zero are considered octal (base 8). Therefore,
012
in octal is equal to10
in decimal. - The decimal value of 0x11 (hexadecimal) is 16 – This is false.
0x11
in hexadecimal actually equals17
in decimal. The correct computation is 1×161+1×160=16+1=17. - Nesting comments is a recommended practice – This is false. In C, nesting comments is not supported by all compilers and can lead to errors or undefined behavior if attempted. Therefore, it is not a recommended practice.
Thus, the true statements are the first four options.
- The “C” language compiler is case-sensitive – This is true. In C, identifiers (including variable names, function names, etc.) are case-sensitive, meaning that
-
The sets of rules that determine the appropriate ways of combining symbols which form correct fragments of code in a programming language are called:
- syntax
- semantics
- lexis
-
Explanation & Hints: The correct term for the sets of rules that determine the appropriate ways of combining symbols to form correct fragments of code in a programming language is syntax.
- Syntax refers to the structure of statements in a programming language, including the correct use of symbols and how commands are arranged. It involves the rules and guidelines that define how code must be written to be correctly interpreted by the compiler or interpreter.
- Semantics, on the other hand, refers to the meaning of those syntactical elements—what the statements actually do when executed.
- Lexis is not a standard term used in programming language context; in linguistic terms, it refers to the vocabulary of a language, which is somewhat analogous to the terminology used in specific programming languages but isn’t directly related to the rules for combining symbols in code.
-
Which line of code must you insert in order for the program to produce the expected output?
Expected output:1
Code:
#include <stdio.h> int main() { int var = 1; print("%d \n", var);
}
placed at the end of the code block
return 0;
placed at the end of the code block
int var;
placed above
int var = 1;
- No change are required – the program will compile and produce the expected output
-
Explanation & Hints: The existing code snippet you provided has a typo in the function used to print the variable
var
. Instead ofprint
, it should beprintf
. Here’s the correct version of your code to produce the expected output:#include <stdio.h> int main() { int var = 1; printf("%d \n", var); return 0; }
So, to answer your question on which line of code must be inserted, “No change are required – the program will compile and produce the expected output” is incorrect due to the typo. The correct function
printf
must be used instead ofprint
. Once you replaceprint
withprintf
, the code will compile and produce the expected output, and includingreturn 0;
at the end of the main function is a good practice to indicate that the program ended successfully.
-
What is the value of the
var
variable after the execution of the following snippet of code:int var = 2; var = 4 var = var + 6; var = var + var;
20
24
22
10
-
Explanation & Hints: Let’s analyze the code snippet and the operations performed on the variable
var
to determine its final value:int var = 2;
initializesvar
with the value2
.var = 4;
changes the value ofvar
to4
.var = var + 6;
adds6
to the current value ofvar
(which is4
), making it10
.var = var + var;
adds the current value ofvar
to itself, so10 + 10 = 20
.
Therefore, after the execution of the code snippet, the value of
var
is 20. Hence, the correct answer is 20.
-
Which of the following identifiers are legal variable names in the C language?
Select three answers.
_3monthsOld
three_months_old
month3
3monthsOld
int
three months
-
Explanation & Hints: In the C programming language, identifiers (including variable names) must follow specific rules:
- Identifiers can consist of letters (both uppercase and lowercase), digits, and underscores.
- Identifiers must not begin with a digit.
- Identifiers must not be keywords of the C language.
- Identifiers should not contain spaces or special characters other than the underscore.
Based on these rules, the legal variable names from the options you’ve provided are:
- three_months_old – This is a legal identifier as it only contains letters and underscores.
- month3 – This is a legal identifier as it starts with a letter and is followed by a digit.
- _3monthsOld – While it is somewhat unconventional to start with an underscore followed by a digit, this is technically a legal identifier in C.
The identifiers that are not legal include:
- 3monthsOld – This is not legal because it starts with a digit.
- int – This is not legal because “int” is a reserved keyword in C.
- three months – This is not legal because it contains a space.
Thus, the three legal variable names are
_3monthsOld
,three_months_old
, andmonth3
.
-
The
return
statement used in a function:- ends the function execution
- continuously repeats the function execution
- repeats the function execution once, and then ends the function execution
-
Explanation & Hints: The return statement used in a function ends the function execution. When a return statement is encountered, the execution of the current function is stopped, and control is returned to the point where the function was called. Additionally, the return statement can be used to send a value back to the caller. This is fundamental in many programming languages, including C, C++, Java, and Python, among others. Thus, the correct understanding of a return statement is that it terminates the function’s execution.
-
Data of type
float
is:- a fractional number
- an integral number
- an internal number
- an integer number
-
Explanation & Hints: Data of type float represents a fractional number. In programming, a
float
typically refers to a floating-point number, which is capable of representing numbers that have a fractional component (numbers with digits beyond the decimal point). This data type is used when more precision is required in calculations involving decimals, unlike integers which represent whole numbers without any fraction or decimal component.
-
What is the output of the following program?
#include <stdio.h> int main() { int var; var = 1; var = 2; /* var = 3; */ print("%d", var); return 0; }
2
3
6
1
-
Explanation & Hints: The output of the provided C program will be 2. Here’s a step-by-step explanation:
int var;
declares an integer variable namedvar
.var = 1;
sets the value ofvar
to 1.var = 2;
then changes the value ofvar
to 2./* var = 3; */
is commented out and thus has no effect on the program’s execution.print("%d", var);
should beprintf("%d", var);
to correctly compile in C. Assuming this is a typo and should beprintf
, it will print the current value ofvar
, which is 2.
Therefore, the correct output, assuming the typo is corrected to
printf
, is 2.