For (A 1 A< 100 A++) Printf (" %d/n", A A)
For (A 1 A< 100 A++) Printf (" %d/n", A A)
For (A 1 A< 100 A++) Printf (" %d/n", A A)
3) Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How
does this aid in debugging?
Placing comment symbols /* */ around a code, also referred to as “commenting out”, is a way of isolating
some codes that you think maybe causing errors in the program, without deleting the code. The idea is
that if the code is in fact correct, you simply remove the comment symbols and continue on. It also saves
you time and effort on having to retype the codes if you have deleted it in the first place.
4) What is the equivalent code of the following statement in WHILE LOOP format?
for (a=1; a<=100; a++)
printf ("%d\n", a * a)
Answer:
1 a=1;
2
3 while (a<=100) {
4
5 printf ("%d\n", a * a);
6
7 a++;
8
9 }
5) What is a stack?
A stack is one form of a data structure. Data is stored in stacks using the FILO (First In Last Out)
approach. At any particular instance, only the top of the stack is accessible, which means that in order to
retrieve data that is stored inside the stack, those on the upper part should be extracted first. Storing data
in a stack is also referred to as a PUSH, while data retrieval is referred to as a POP.
6) What is a sequential access file?
When writing programs that will store and retrieve data in a file, it is possible to designate that file into
different forms. A sequential access file is such that data are saved in sequential order: one data is placed
into the file after another. To access a particular data within the sequential access file, data has to be read
one data at a time, until the right one is reached.
10) In C programming, how do you insert quote characters (‘ and “) into the output screen?
This is a common problem for beginners because quotes are normally part of a printf statement. To insert
the quote character as part of the output, use the format specifiers \’ (for single quote), and \” (for double
quote).
15) Which of the following operators is incorrect and why? ( >=, <=, <>, ==)
<> is incorrect. While this operator is correctly interpreted as “not equal to” in writing conditional
statements, it is not the proper operator to be used in C programming. Instead, the operator != must be
used to indicate “not equal to” condition.
17) How do you declare a variable that will hold string values?
The char keyword can only hold 1 character value at a time. By creating an array of characters, you can
store string values in it. Example: “char MyName[50]; ” declares a string variable named MyName that can
hold a maximum of 50 characters.
18) Can the curly brackets { } be used to enclose a single line of code?
While curly brackets are mainly used to group several lines of codes, it will still work without error if you
used it for a single line. Some programmers prefer this method as a way of organizing codes to make it
look clearer, especially in conditional statements.
19) What are header files and what are its uses in C programming?
Header files are also known as library files. They contain two essential things: the definitions and
prototypes of functions being used in a program. Simply put, commands that you use in C programming
are actually functions that are defined from within each header files. Each header file contains a set of
functions. For example: stdio.h is a header file that contains definition and prototypes of commands like
printf and scanf.
21) What are variables and it what way is it different from constants?
Variables and constants may at first look similar in a sense that both are identifiers made up of one
character or more characters (letters, numbers and a few allowable symbols). Both will also hold a
particular value. Values held by a variable can be altered throughout the program, and can be used in
most operations and computations. Constants are given values at one time only, placed at the beginning
of a program. This value is not altered in the program. For example, you can assigned a constant named
PI and give it a value 3.1415 . You can then use it as PI in the program, instead of having to write 3.1415
each time you need it.
23) Can I use “int” data type to store the value 32768? Why?
No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can use “long
int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative values.
24) Can two or more operators such as \n and \t be combined in a single line of program code?
Yes, it’s perfectly valid to combine operators, especially if the need arises. For example: you can have a
code like ” printf (“Hello\n\n\’World\'”) ” to output the text “Hello” on the first line and “World” enclosed in
single quotes to appear on the next two lines.
25) Why is it that not all header files are declared in every C program?
The choice of declaring a header file at the top of each C program would depend on what
commands/functions you will be using in that program. Since each header file contains different function
definitions and prototype, you would be using only those header files that would contain the functions you
will need. Declaring all header files in every program would only increase the overall file size and load of
the program, and is not considered a good programming style.
30) Write a loop statement that will show the following output:
1
12
123
1234
12345
Answer:
An ampersand & symbol must be placed before the variable name whatnumber. Placing & means
whatever integer value is entered by the user is stored at the “address” of the variable name. This is a
common mistake for programmers, often leading to logical errors.
33) What could possibly be the problem if a valid function name such as tolower() is being reported by the C
compiler as undefined?
The most probable reason behind this error is that the header file for that function was not indicated at the
top of the program. Header files contain the definition and prototype for functions and commands used in
a C program. In the case of “tolower()”, the code “#include <ctype.h>” must be present at the beginning of
the program.
37) In C programming, what command or code can be used to determine if a number of odd or even?
There is no single command or function in C that can check if a number is odd or even. However, this can
be accomplished by dividing that number by 2, then checking the remainder. If the remainder is 0, then
that number is even, otherwise, it is odd. You can write it in code as:
1 if (num % 2 == 0)
2
3 printf("EVEN");
4
5 else
6
7 printf("ODD");
38) What does the format %10.2 mean when included in a printf statement?
This format is used for two things: to set the number of spaces allotted for the output number and to set
the number of decimal places. The number before the decimal point is for the allotted space, in this case it
would allot 10 spaces for the output number. If the number of space occupied by the output number is
less than 10, addition space characters will be inserted before the actual output number. The number after
the decimal point sets the number of decimal places, in this case, it’s 2 decimal spaces.
39) What are logical errors and how does it differ from syntax errors?
Program that contains logical errors tend to pass the compilation process, but the resulting output may not
be the expected one. This happens when a wrong formula was inserted into the code, or a wrong
sequence of commands was performed. Syntax errors, on the other hand, deal with incorrect commands
that are misspelled or not recognized by the compiler.
44) What will be the outcome of the following conditional statement if the value of variable s is 10?
s >=10 && s < 25 && s!=12
The outcome will be TRUE. Since the value of s is 10, s >= 10 evaluates to TRUE because s is not
greater than 10 but is still equal to 10. s< 25 is also TRUE since 10 is less then 25. Just the same, s!=12,
which means s is not equal to 12, evaluates to TRUE. The && is the AND operator, and follows the rule
that if all individual conditions are TRUE, the entire statement is TRUE.
45) Describe the order of precedence with regards to operators in C.
Order of precedence determines which operation must first take place in an operation statement or
conditional statement. On the top most level of precedence are the unary operators !, +, – and &. It is
followed by the regular mathematical operators (*, / and modulus % first, followed by + and -). Next in line
are the relational operators <, <=, >= and >. This is then followed by the two equality operators == and !=.
The logical operators && and || are next evaluated. On the last level is the assignment operator =.
47) How do you determine the length of a string value that was stored in a variable?
To get the length of a string value, use the function strlen(). For example, if you have a variable named
FullName, you can get the length of the stored string value by using this statement: I = strlen(FullName);
the variable I will now have the character length of the string value.
50) What are the different file extensions involved when programming in C?
Source codes in C are saved with .C file extension. Header files or library files have the .H file extension.
Every time a program source code is successfully compiled, it creates an .OBJ object file, and an
executable .EXE file.
55) Not all reserved words are written in lowercase. TRUE or FALSE?
FALSE. All reserved words must be written in lowercase; otherwise the C compiler would interpret this as
unidentified and invalid.
56) What is the difference between the expression “++a” and “a++”?
In the first expression, the increment would happen first on variable a, and the resulting value will be the
one to be used. This is also known as a prefix increment. In the second expression, the current value of
variable a would the one to be used in an operation, before the value of a itself is incremented. This is
also known as postfix increment.
57) What would happen to X in this expression: X += 15; (assuming the value of X is 5)
X +=15 is a short method of writing X = X + 15, so if the initial value of X is 5, then 5 + 15 = 20.
58) In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?
FALSE. C language is a case sensitive language. Therefore, NAME, name and Name are three uniquely
different variables.
60) What is a program flowchart and how does it help in writing a program?
A flowchart provides a visual representation of the step by step procedure towards solving a given
problem. Flowcharts are made of symbols, with each symbol in the form of different shapes. Each shape
may represent a particular entity within the entire program structure, such as a process, a condition, or
even an input/output phase.
70) Write a simple code fragment that will check if a number is positive or negative.
1 If (num>=0)
2
3 printf("number is positive");
4
5 else
6
7 printf ("number is negative");
The switch statement is best used when dealing with selections based on a single variable or expression.
However, switch statements can only evaluate integer and character data types.
72) What are global variables and how do you declare them?
Global variables are variables that can be accessed and manipulated anywhere in the program. To make
a variable global, place the variable declaration on the upper portion of the program, just after the
preprocessor directives section.
79) Dothese two program statements perform the same output? 1) scanf(“%c”, &letter); 2) letter=getchar()
Yes, they both do the exact same thing, which is to accept the next key pressed by the user and assign it
to variable named letter.
81) What does the characters “r” and “w” mean when writing programs that will make use of files?
“r” means “read” and will open a file as input wherein data is to be retrieved. “w” means “write”, and will
open a file for output. Previous data that was stored on that file will be erased.
82) What is the difference between text files and binary files?
Text files contain data that can easily be understood by humans. It includes letters, numbers and other
characters. On the other hand, binary files contain 1s and 0s that only computers can interpret.
93) The % symbol has a special use in a printf statement. How would you place this character as part of the
output on the screen?
You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have
the output appear as 10% on the screen.
94) How do you search data in a data file using random access method?
Use the fseek() function to perform random access input/ouput on a file. After the file was opened by the
fopen() function, the fseek would require three parameters to work: a file pointer to the file, the number of
bytes to search, and the point of origin in the file.
95) Are comments included during the compilation stage and placed in the EXE file as well?
No, comments that were encountered by the compiler are disregarded. Comments are mostly for the
guidance of the programmer only and do not have any other significant use in the program functionality.
96) Is there a built-in function in C that can be used for sorting data?
Yes, use the qsort() function. It is also possible to create user defined functions for sorting, such as those
based on the balloon sort and bubble sort algorithm.
99) Create a simple code fragment that will swap the values of two variables num1 and num2.
1 int temp;
2
3 temp = num1;
4
5 num1 = num2;
6
7 num2 = temp;
100) What is the use of a semicolon (;) at the end of every program statement?
It has to do with the parsing process and compilation of the code. A semicolon acts as a delimiter, so that
the compiler knows where each statement ends, and can proceed to divide the statement into smaller
elements for syntax checking.