List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.
C Interview Questions in Java
Question 1. What is the C language?
Answer:
- C language is a structure/procedure-oriented, middle-level programming language developed at Bell Laboratories in 1972 by Dennis Ritchie.
- C language was invented for implementing UNIX operating system.
- In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming Language”.
- Also, C language is an ANSI/ISO standard and powerful programming language for developing real-time applications
Question 2.
Who developed the C language?
Answer:
C programming language was developed at Bell Laboratories in 1972 by Dennis Ritchie.
Question 3.
Where is the C programming language used or uses of the C language?
Answer:
C language is used to develop system applications that form the major portion of operating systems such as Windows, UNIX, and Linux. Operating systems, C compiler, and all UNIX application programs are written in C language. Below are some examples of uses of the C language.
- Database systems
- Graphics packages
- Word processors
- Spreadsheets
- Operating system development
- Compilers and Assemblers
- Network drivers
- Interpreters
Question 4.
What is the difference between C and C++?
Answer:
Even though C and C++ programming languages are belonging to middle
level languages, both are differed in below.
- C is a structure/procedure-oriented programming language whereas C++ is an object-oriented programming language.
- C language program design is a top-down approach whereas C++ is using a bottom-up approach.
- Polymorphism, virtual function, inheritance, Operator overloading, namespace concepts are not available in the C programming language. Whereas C++ language supports all these concepts and features.
- C language gives importance to functions rather than data. Whereas C++ gives importance to data rather than functions.
- So, data and function mapping are difficult in C. But, data and function mapping are simple in C++ that can be done using objects.
- C language does not support user define data types. Whereas C++ supports user define data types.
- Exception handling is not present in the C programming language. Whereas exception handling is present in the C++ language.
- C language allows data to freely flow around the functions. But, data and functions are bound together in C++ which does not allow data to freely flow around the functions.
Question 5.
What is the difference between the top-down approach and the bottom-up approach in programming languages?
Answer:
The top-down approach and bottom-up approach are involved in software
development. These approaches are not involved in program execution.
- Structure/procedure-oriented programming languages like the C programming language follow a top-down approach. Whereas object-oriented programming languages like C++ and Java programming language follow a bottom-up approach.
- The top-down approach begins with high-level design and ends with low-level design or development. Whereas, the bottom-up approach begins with low-level design or development and ends with high-level design.
- In the top-down approach, the main( ) function is written first and all sub-functions are called from the main function. Then, sub-functions are written based on the requirement. Whereas, in the bottom-up approach, code is developed for modules and then these modules are integrated with the main( ) function.
- Now-a-days, both approaches are combined together and followed in modern software design.
Question 6.
What is the difference between C and Java?
Answer:
- C is a structure/procedure-oriented programming language whereas Java is an object-oriented programming language.
- C language program design is a top-down approach whereas Java is using a bottom-up approach.
- C language is a middle-level language whereas Java is a high-level language.
- Exception handling is not present in the C programming language. Whereas exception handling is present in Java.
- Polymorphism, virtual function, inheritance, Operator overloading, namespace concepts are not available in the C programming language. Whereas Java supports all these concepts and features.
Question 7.
C language has been developed in which language?
Answer:
C language has been developed using assembly-level language.
Question 8.
Which year C language is developed?
Answer:
C programming language was developed at Bell Laboratories in 1972 by Dennis Ritchie.
Question 9.
What is meant by programming language and give some examples?
Answer:
- Programming language is nothing but a language designed to communicate to machines through instructions and commands.
- Normally machines are computers. Programs are written using some programming languages to control the behavior of machines/computers and to make them perform required tasks.
- Programming language example: Assembly language, C language, C++ language, Java, C#, .NET, Python, etc.,
Question 10.
Describe C standards.
Answer:
- C89/C90 standard—First standardized specification for C language was developed by American National Standards Institute in 1989. C89 and C90 standards refer to the same programming language.
- C99 standard – Next revision was published in 1999 that introduced new futures like advanced data types and other changes.
- C11 standard adds new features to C and library like type generic macros, anonymous structures, improved Unicode support, atomic operations, multi¬threading, and bounds-checked functions. It also makes some portions of the existing C99 library optional and improves compatibility with C++.
Question 11.
What are the key features or characteristics of the C language?
Answer:
- Reliability
- Portability
- Flexibility
- Interactivity
- Modularity
- Efficiency and Effectiveness
Question 12.
What is embedded C?
Answer:
- Embedded C is the extension of the C programming language.
- Embedded C is used to develop microcontroller-based applications.
- Embedded C includes features not available in normal C like fixed-point arithmetic, named address spaces, and basic I/O hardware addressing.
- Cell phones, MP3 players are some examples of embedded systems in which embedded C is used to program and control these devices.
Question 13.
Which level is the C language belonging to?
Answer:
- C language is belonging to middle-level language. C language behaves as a bridge between machine-level (low-level) languages and high-level languages.
- C language is more user-friendly than machine-level languages. And, C language does not support all the concepts that high-level languages offer. So, the C programming language is called middle-level language.
Question 14.
What do you mean by high level, middle level, and low-level languages, and give an example for each?
Answer:
High-level languages –
- These level languages provide almost everything that the programmer might need to do as already build into the language.
- Example: Java, Python
Middle-level languages –
- These languages don’t provide all the built-in functions found in high-level languages but provide all the building blocks that we need to produce the result we want.
- Example: C, C++
Low-level languages –
- These languages provide nothing other than access to the machine’s basic instruction set.
- Example: Assembly language
Question 15.
What is the difference between structured-oriented, object-oriented, and non-structure-oriented programming languages?
Answer:
Structured oriented programming language –
- In this type of language, large programs are divided into small programs called functions.
- The prime focus is on functions and procedures that operate on data
- Data moves freely around the systems from one function to another
- Program structure follows “Top-Down Approach”
- Example: C, Pascal, ALGOL, and Modula-2
Object-oriented programming language –
- In this type of language, programs are divided into objects
- The prime focus is on the data that is being operated and not on the functions or procedures
- Data is hidden and cannot be accessed by external functions
- Program structure follows “Bottom UP Approach”
- Example: C++, JAVA, and C# (C sharp)
Non-structure oriented programming language –
- There is no specific structure for programming this language.
- Example: BASIC, COBOL, FORTRAN
- Assembler is a program that converts assembly-level language (low-level language) into machine-level language.
- The compiler compiles the entire C source code into machine code. Whereas interpreters convert source code into intermediate code and then this intermediate code is executed line by line.
- printf( ) is an inbuilt library function in C which is available in the C library by default. This function is declared and related macros are defined in “stdio.h” header file.
- printf( ) function is used to print the “character, string, float, integer, octal, and hexadecimal values” onto the output screen.
- scanf( ) function is an inbuilt library function in C that is available in the C library by default. This function is declared and related macros are defined in “stdio.h” header file.
- scanf( ) function is used to read character, string, numeric data from the keyboard.
- Documentation section
- Link Section
- Definition Section
- Global declaration section
- Function prototype declaration section
- Main function
- User-defined function section
- IDE is nothing but Integrated Development Environment. IDE is a tool that provides a user interface with compilers to create, compile and execute C programs.
- Example: Turbo C++, Borland C++, and DevC++. These provide Integrated Development Environment with a compiler for both C and C++ programming languages.
- AMPC
- CCS C Compiler
- ch
- clang
- Cygwin
- Digital mars
- GCC compiler
- MikroC Compiler
- Portable C Compiler, Power C, QuickC, Ritchie C Compiler, Small-C
- The header file is a file that contains function declaration and macro definition for C in-built library functions.
- All C standard library functions are declared in many header files which are saved as file_name.h.
- We are including these header files in our C program using the “#include <file_name. h>” command to make use of the functions that are declared in the header files.
- When we include header files in our C program using the “#include <filename.h>” command, all C codes of the header files are included in the C program. Then, this C program is compiled by the compiler and executed.
- Data types in C language are defined as the data storage format that a variable that can store data to perform a specific operation.
- Data types are used to define a variable before use in a program.
- Size of variable, constant, and array are determined by data types.
- The integer data type allows a variable to store numeric values. The storage size of int data type is 2 or 4 or 8 bytes. It varies depending upon the processor in the CPU.
- Character data type allows a variable to store only one character. The storage size of the character data type is 1.
- The float data type allows a variable to store decimal values. The storage size of float data type is 4. This also varies depending upon the processor in the CP.
- The double data type is also the same as the float data type which allows up to 10 digits after the decimal.
- The amount of memory space to be allocated for a variable is derived by modifiers.
- Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
- For example, storage space for the int data type is 4 bytes for the 32-bit processor. We can increase the range by using long int which is 8 bytes. We can decrease the range by using short int which is 2 bytes.
- short
- long
- signed
- unsigned
- long long
- Enumeration is a data type that consists of named integer constants as a list.
- It starts with 0 (zero) by default and the value is incremented by 1 for the sequential identifiers in the list.
- We use void data type in functions when we don’t want to return any value to the calling function.
- We use void data type in pointer like “void *p”. It means pointer “p” is neither ‘ pointing to int data type nor char data type. It acts as a generic pointer. We are using a void pointer when we are not sure on the data type that this pointer will point to. We can use the void pointer to refer to either integer data or char data. But, this void pointer should not be dereferenced without explicit typecasting.
- We use void in functions as “int function_name (void)”. Here void means, this function does not pass any argument.
- C tokens are the basic buildings blocks in the C language which are constructed together to write a C program.
- Each and every smallest individual unit in a C program are known as C tokens.
- keywords (eg: int, while),
- Identifiers (eg: main, total),
- Constants (eg: 10, 20),
- Special symbols (eg: “total”, “hello”),
- Special Symbols (eg: 0, 0),
- Operators (eg: +, /,-,*)
- Each program element in a C program is given a name called identifiers.
- Names given to identify Variables, functions and arrays are examples of identifiers.
- Example: x is a name given to an integer variable in the above program
- Keywords are pre-defined words in a C compiler. Each keyword is meant to perform a specific function in a C program.
- Since keywords are referred names for compilers, they can’t be used as variable names.
- Below are some of the keywords that C language offers.
- auto, double, int, struct, break, else, long, switch, case, enum, register, typedef, char, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, size, volatile, do, if, static, while
- Constants refer to fixed values. They are also called literals.
- C Constants are also like normal variables. But, the only difference is, constant values can’t be modified by the program once they are defined. Constants may be belonging to any of the data types.
- Integer constants
- Real or Floating-point constants
- Octal & Hexadecimal constants
- Character constants
- String constants
- Backslash character constants
- C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.
- The value of the C variable may get change in the program.
- C variable might be belonging to any of the data types like int, float, char, etc.
- the variable declaration tells the compiler about the data type and size of the variable. Whereas, variable definition allocates memory to the variable
- Variable can be declared many times in a program. But, the definition can happen only one time for a variable in a program.
- Variable declaration is for the assignment of properties and identification to a variable. Whereas, a variable definition is for assignments of storage space to a variable
Answer:
- Local variable
- Global variable
- Environment variable
What is the local variable in C?
- The variables which are having scope/life only within the function are called local variables.
- These variables are declared within the function and can’t be accessed outside the function.
What is the global variable in C?
- The variables which are having scope/life throughout the program are called global variables.
- A global variable is defined outside the main function. So, this variable is visible to the main function and all other sub-functions.
- The environment variable is a variable that will be available for all C applications and C programs.
- Once environment variables are exported, we can access them from anywhere in a C program without declaring and initializing them in an application or C program.
- The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
- These C operators join individual constants and variables to form expressions. Operators, functions, constants, and variables are combined together to form expressions.
- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bitwise operators
- Conditional operators (ternary operators)
- Increment/decrement operators
- Special operators
- The ternary operator is the same as the if-else control statement in C.
- Syntax : (Condition? true_value: false_value);
- Example: (A > 100? 0: 1);
- C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus in C programs.
- Arithmetic operators are +, -, *, /, %
- Assignment operators are used to assigning the values to the variables.
- Assignment operators are =, +=, -=, /+, %= etc.
What is the relational operator in C?
- Relational operators are used to finding the relation between two variables, i.e. to compare the values of two variables in a C program.
- Relational operators are >, <, >=, <=, ==, !=
- Logical operators are used to performing logical operations on the given expressions.
- There are 3 logical operators in the C language. They are,
3. logical NOT (!)
Question 56.
What is a bitwise operator in C?
Answer:
- Bitwise operators are used to performing bit operations. Decimal values are
converted into binary values which are the sequence of bits and bit-wise operators work on these bits. - Bitwise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), A (XOR), << (left shift) and >> (right shift).
Question 57.
Differentiate between the expression “++a” and “a++”?
Answer:
- With ++a, the increment happens first on variable a, and the resulting value is used. This is called prefix increment.
- With a++, the current value of the variable will be used in an operation. This is called a postfix increment.
Question 58.
What are all decision control statements in C?
Answer:
There are 3 types of decision-making control statements in C language. They are,
- if statements
- if-else statements
- nested if statements
Question 59.
What are all loop control statements in C?
Answer:
- Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once the condition becomes false.
- There are 3 types of loop control statements in C language. They are,
1. for
2. while
3. do-while
Question 60.
What are the difference between while and do-while loops in C?
Answer:
- While loop is executed only when a given condition is true.
- Whereas, do-while loop is executed for the first time irrespective of the condition. After executing the while loop for the first time, the condition is checked.
Question 61.
What is the difference between single equal “=” and double equal “=” operators in C?
Answer:
- Single equal is an assignment operator used to assign the values to the variables.
- But, double equal is the relational operator used to compare two variable values whether they are equal are not.
Question 62.
What is the difference between the pre-increment operator and post-increment operator in C?
Answer:
- The pre-increment operator is used to increment variable value by 1 before assigning the value to the variable.
- The post-increment operator is used to increment variable value by 1 after assigning the value to the variable.
Question 63.
What is the difference between the pre decrement operator and the post decrement operator?
Answer:
- Pre decrement operator is used to decrement the variable value by 1 before assigning the value to the variable.
- Post decrement operator is used to decrement the variable value by 1 after assigning the value to the variable.
Question 64.
What are “&” and operators in C?
Answer:
- The operator is used as a pointer to a variable. Example: * a where * is a pointer to the variable a.
- & operator is used to getting the address of the variable. Example: &a will give the address of a.
Question 65.
What will happen if the break statement is not used in the switch case in C?
Answer:
- Switch case statements are used to execute only specific case statements based on the switch expression.
- If we do not use a break statement at the end of each case, the program will execute all consecutive case statements until it finds the next break statement or till the end of the switch case block.
Question 66.
Why is the default statement used in the switch case in C?
Answer:
Switch case statements are used to execute only specific case statements based on the switch expression. If the switch expression does not match with any case, default statements are executed by the program.
Question 67.
What is the use of the “goto” statement?
Answer:
the goto statement is used to transfer the normal flow of a program to the specified labl in the program. Below is the syntax for the goto statement in C.
{ ..... . goto label; ..... . ..... . LABEL: statements; }
Question 68.
What value will be assigned to the variable X if a = 10, b = 20, c = 30, d = 40 in below expression?
Answer:
X = a/b+c*d-c;
- The above arithmetic operation is performed based on the precedence of the operators.
- In above mentioned expression, c*d will be performed first. Then, a/b, then (c*d)-c, then (a/b) + ((c*d)-c).
- Please check the operator precedence table to know the priority and associativity of the C operators.
- The output of the above expression is 1170.
Example program:
#include <stdio.h> #include <stdlib.h> int main( ) { int a = 10, b = 20, c = 30, d = 40, X ; x = a/b+c*d-c;. printf(“ value of x = %d\n”, x); return 0; }
Output: Value of X = 1170
Question 69.
What value is assigned to the below variables?
Answer:
int Xl = 13/3;
mt X2 = 13%3;
- The value of XI is 4
- The value of X2 is 1
Question 70.
What is the difference between auto variable and register variable in C?
Answer:
Storage class of all variables are auto by default unless we specify a variable is register or static or extern in C program.
- Both auto variables and register variables are local variables. Register variables are stored in register memory. Whereas, auto variables are stored in main CPU memory.
- Register variables will be accessed very faster than the normal/auto variables since they are stored in register memory rather than main memory.
- But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits)
Question 71.
What is the difference between auto variable and static variable in C?
Answer:
- Both auto and static variables are local variables.
- Static variables can retain the value of the variable between different function calls.
- But, scope of auto variable is within the function only. It can’t retain the value of the variable between different function calls.
Question 72.
Where should type cast function not be used in C?
Answer:
- Typecast function should not be used in const and volatile declaration.
- Because constant values can’t be modified by the program once they are defined.
- And, volatile variable values might keep on changing without any explicit assignment by the program as operating system will be modifying these values.
Question 73.
How will you print “Hello World” without semicolon?
Answer:
int main(void){ if (printf(“Hello world”)) ; }
Question 74.
Why is C language being considered a middle level language?
Answer:
This is because C language is rich in features that make it behave like a high level language while at the same time can interact with hardware using low level methods. The use of a well structured approach to programming, coupled with English-like words used in functions, makes it act as a high level language. On the other hand, C can directly access memory structures similar to assembly language routines.
Question 75.
What are the different file extensions involved when programming in C?
Answer:
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.
Question 76.
What are reserved words?
Answer:
Reserved words are words that are part of the standard C language library. This means that reserved words have special meaning and therefore cannot be used for purposes other than what it is originally intended for. Examples of reserved words are int, void, and return.
Question 77.
Why is c considered as a high level language and a low level language?
Answer:
C is a high-level language because it abstracts away a large part of the underlying machine; instead of dealing with registers and op codes, you are dealing with objects and expressions. Streams are abstractions of communications channels, pointers are abstractions of memory addresses, etc. However, Cs abstractions are fairly basic; you still have to be aware of limitations on memory, you have to be aware of how types and operations may be mapped onto the underlying hardware, and you have to be aware that some aspects of the language are left to the individual implementation (compiler or interpreter) to define.
C makes you *think* you are working at the hardware level, even though you aren’t in most cases. Compare this with languages like Haskell or Lisp, where there is no hint of an underlying hardware platform; types are not limited by what an individual platform can represent (as opposed to C), there are no obvious constraints on memory (until performance begins to suffer, anyway).
Question 78.
What is the difference between getch( ) and getche( )?
Answer:
Both getch( ) and getche( ) are used to read single character there is very little difference
- Both functions accept a character input value from the user.
- When getch( ) is used, the key that was pressed will not appear on the screen. It is automatically captured and assigned to a variable.
- While when getche( ) is used,, the key that was pressed by the user appears on the screen and is assigned to a variable.
Question 79.
How do you construct an increment statement or decrement statement in C?
Answer:
There are actually two ways you can do this. One is to use the increment operator ++ and decrement operator For example, the statement “x++” means to increment the value of x b}- 1. Likewise; the statement “x -” means to decrement the value of x by 1. Another way of writing increment statements is to use the conventional + plus sign or – minus sign. In the case of “x++”, another way to write it is “x = x +1″.
Question 80.
Are comments included during the compilation stage and placed in the EXE file as well?
Answer:
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.
Question 81.
What is a sequential access file?
Answer:
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.
Question 82.
What is variable initialization and why is it important?
Answer:
This refers to the process wherein a variable is assigned an initial value before it is used in the program. Without initialization, a variable would have an unknown value, which can lead to unpredictable outputs when used in computations or other operations.
Question 83.
What is spaghetti programming?
Answer:
Spaghetti programming refers to codes that tend to get tangled and overlapped throughout the program. This unstructured approach to coding is usually attributed to lack of experience on the part of the programmer. Spaghetti programing makes a program complex and analyzing the codes difficult, and so must be avoided as much as possible.
Question 84.
Differentiate Source Codes.from Object Codes
Answer:
Source codes are codes that were written by the programmer. It is made up of the commands and other English-like keywords that are supposed to instruct the computer what to do. However, computers would not be able to understand source codes. Therefore, source codes are compiled using a compiler. The resulting outputs are object codes, which are in a format that can be understood by the computer processor. In C programming, source codes are saved with the file extension .C, while object codes are saved with the file extension .OBJ
int x=64; if(x&&(x&(x-l))) printf(“ x is not the power of 2”); else printf(“x is power of 2”);
When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then “void” is to be placed at the leftmost part of the function header. When a return value is expected after the function execution, the data type of the return value is placed instead of “void”.
Question 105.
What are compound statements?
Answer:
Compound statements are made up of two or more program statements that are executed together. This usually occurs while handling conditions wherein a series of statements are executed when a TRUE or FALSE is evaluated. Compound statements can also be executed within a loop. Curly brackets { } are placed before and after compound statements.
Question 106.
What is the significance of an algorithm to C programming?
Answer:
Before a program can be written, an algorithm has to be created first. An algorithm provides a step by step procedure on how a solution can be derived. It also acts as a blueprint on how a program will start and end, including what process and computations are involved.
Question 107.
What is the advantage of an array over individual variables?
Answer:
When storing multiple related data, it is a good idea to use arrays. This is because arrays are named using only 1 word followed by an element number. For example: to store the 10 test results of 1 student, one can use 10 different variable names (grade 1, grade2, grade3… grade 10). With arrays, only 1 name is used, the rest are accessible through the index name (grade[0], grade[1], grade[2]… grade[9]).
Question 108.
Can a variable be both const and volatile?
Answer:
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. If a variable is both const and volatile, the two modifiers can appear in either order.
Question 109.
What is wrong in this statement? scanf(“%d”,whatnumber);
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.
Question 110.
How do you generate random numbers in C?
Answer:
Random numbers are generated in C using the rand( ) command. For example: anyNum = randO will generate any integer number beginning from 0, assuming that anyNum is a variable of type integer.
Question 111.
What could possibly be the problem if a valid function name such as to lower( ) is being reported by the C compiler as undefined?
Answer:
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.
Question 112.
What are comments and how do you insert it in a C program?
Answer:
Comments are a great way to put some remarks or description in a program. It can serves as a reminder on what the program is all about, or a description on why a certain code or function was placed there in the first place. Comments begin with /* and ended by */ characters. Comments can be a single line, or can even span several fines. It can be placed anywhere in the program.
Question 113.
What is debugging?
Answer:
Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely. At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors, and plays an important role in ensuring that the expected program output is met.
Question 114.
What does the && operator do in a program code?
Answer:
The && is also referred to as AND operator. When using this operator, all conditions specified must be TRUE before the next action can be performed. If you have 10 conditions and all but 1 fails to evaluate as TRUE, the entire condition statement is already evaluated as FALSE.
Question 115.
What does the format %10.2 mean when included in a printf statement?
Answer:
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, its 2 decimal spaces.
Question 116.
What are logical errors and how does it differ from syntax errors?
Answer:
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.
Question 117.
What are the different types of control structures in programming?
Answer:
There are 3 main control structures in programming: Sequence, Selection and Repetition. Sequential control follows a top to bottom flow in executing a program, such that step 1 is first perform, followed by step 2, all the way until the last step is performed. Selection deals with conditional statements, which mean codes are executed depending on the evaluation of conditions as being TRUE or FALSE. This also means that not all codes may be executed, and there are alternative flows within. Repetitions are also known as loop structures, and will repeat one or two program statements set by a counter.
Question 118.
What is || operator and how does it function in a program?
Answer:
The | | is also known as the OR operator in C programming. When using | | to evaluate logical conditions, any condition that evaluates to TRUE will render the entire condition statement as TRUE.
Question 119.
Not all reserved words are written in lowercase. TRUE or FALSE?
Answer:
FALSE. All reserved words must be written in lowercase; otherwise the C compiler would interpret this as unidentified and invalid.
Question 120.
What is the difference between the expression “++a” and “a++”?
Answer:
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.
Question 121.
What would happen to X in this expression: X += 15; (assuming the value of X is 5)
Answer:
X +=15 is a short method of writing X = X + 15, so if the initial value of X is 5, then 5 + 15 = 20.
Question 122.
In C language, the variables NAME, name, and Name are all the same. TRUE or FALSE?
Answer:
FALSE. C language is a case sensitive language. Therefore, NAME, name and Name are three uniquely different variables.
Question 123.
What is an endless loop?
Answer:
An endless loop can mean two things. One is that it was designed to loop continuously until the condition within the loop is met, after which a break function would cause the program to step out of the loop. Another idea of an endless loop is when an incorrect loop condition was written, causing the loop to run erroneously forever. Endless loops are oftentimes referred to as infinite loops.
Question 124.
Can the “if’ function be used in comparing strings?
Answer:
No. “if’ command can only be used to compare numerical values and single character values. For comparing string values, there is another function called strcmp that deals specifically with strings.
Question 125.
What is a program flowchart and how does it help in writing a program?
Answer:
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.
Question 126.
What is wrong with this program statement? void = 10;
Answer:
The word void is a reserved word in C language. You cannot use reserved words as a user-defined variable.
Question 127.
Is this program statement valid? INT = 10.50;
Answer:
Assuming that INT is a variable of type float, this statement is valid. One may think that INT is a reserved word and must not be used for other purposes. However, recall that reserved words are express in lowercase, so the C compiler will not interpret this as a reserved word.
Question 128.
What is a newline escape sequence?
Answer:
A newline escape sequence is represented by the \n character. This is used to insert a new line when displaying data in the output screen. More spaces can be added by inserting more \n characters. For example, \n\n would insert two spaces. A newline escape sequence can be placed before the actual output expression or after.
Question 129.
Is it possible to initialize a variable at the time it was declared?
Answer:
Yes, you don’t have to write a separate assignment statement after the variable declaration, unless you plan to change it later on. For example: char planet[15] = “Earth”; does two things: it declares a string variable named planet, then initializes it with the value “Earth”.
Question 130.
What is output redirection?
Answer:
It is the process of transferring data to an alternative output source other than the display screen. Output redirection allows a program to have its output saved to a file. For example, if you have a program named COMPUTE, typing this on the command fine as COMPUTE >DATA can accept input from the user, perform certain computations, then have the output redirected to a file named DATA, instead of showing it on the screen.
Question 131.
What are run-time errors?
Answer:
These are errors that occur while the program is being executed. One common instance wherein run-time errors can happen is when you are trying to divide a number by zero. When run-time errors occur, program execution will pause, showing which program line caused the error.
Question 132.
What is the difference between functions abs( ) and fabs( )?
Answer:
These 2 functions basically perform the same action, which is to get the absolute value of the given value. AbsO is used for integer values, while fabs( ) is used for floating type numbers. Also, the prototype for abs( ) is under <stdlib.h>, while fabs( ) is under <math.h>.
Question 133.
What are control structures?
Answer:
Control structures take charge at which instructions are to be performed in a program. This means that program flow may not necessarily move from one statement to the next one, but rather some alternative portions may need to be pass into or bypassed from, depending on the outcome of the conditional statements.
Question 134.
When is a “switch” statement preferable over an “if’ statement?
Answer:
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.
Question 135.
What are global variables and how do you declare them?
Answer:
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.
Question 136.
What are enumerated types?
Answer:
Enumerated types allow the programmer to use more meaningful words as values to a variable. Each item in the enumerated type variable is actually associated with a numeric code. For example, one can create an enumerated type variable named DAYS whose values are Monday, Tuesday… Sunday.
Question 137.
What does the function toupper( ) do?
Answer:
It is used to convert any letter to its upper case mode. Toupper( ) function prototype is declared in <ctype.h>. Note that this function will only convert a single character, and not an entire string.
Question 138.
Is it possible to have a function as a parameter in another function?
Answer:
Yes, that is allowed in C programming. You just need to include the entire function prototype into the parameter field of the other function where it is to be used.
Question 139.
What are multidimensional arrays?
Answer:
Multidimensional arrays are capable of storing data in a two or more dimensional structure. For example, you can use a 2 dimensional array to store the current position of pieces in a chess game, or position of players in a tic-tac-toe program.
Question 140.
Which function in C can be used to append a string to another string?
Answer:
The strcat function. It takes two parameters, the source string and the string value to be appended to the source string.
Question 141.
What is the difference between functions getch( ) and getche( )?
Answer:
Both functions will accept a character input value from the user. When using getch( ), the key that was pressed will not appear on the screen, and is automatically captured and assigned to a variable. When using getche( ), the key that was pressed by the user will appear on the screen, while at the same time being assigned to a variable.
Question 142.
Do these two program statements perform the same output? 1) scanf(“%c”, &letter); 2) letter=getchar()
Answer:
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.
Question 143.
What are structure types in C?
Answer:
Structure types are primarily used to store records. A record is made up of related fields. This makes it easier to organize a group of related data.
Question 144.
Is it possible to create your own header files?
Answer:
Yes, it is possible to create a customized header file. Just include in it the function prototypes that you want to use in your program, and use the #include directive followed by the name of your header file.
Question 145.
What is dynamic data structure?
Answer:
Dynamic data structure provides a means for storing data more efficiently into memory. Using dynamic memory allocation, your program will access memory spaces as needed. This is in contrast to static data structure, wherein the programmer has to indicate a fix number of memory space to be used in the program.
Question 146.
What are the different data types in C?
Answer:
The basic data types are int, char, and float. Int is used to declare variables that will be storing integer values. Float is used to store real numbers. Char can store individual character values.
Question 147.
What is the general form of a C program?
Answer:
A-C program begins with the preprocessor directives, in which the programmer would specify which header file and what constants (if any) to be used. This is followed by the main function heading. Within the main function lies the variable declaration and program statement.
Question 148.
What is the advantage of a random access file?
Answer:
If the amount of data stored in a file is fairly large, the use of random access will allow you to search through it quicker. If it had been a sequential access file, you would have to go through one record at a time until you reach the target data. A random-access file lets you jump directly to the target address where data is located.
Question 149.
In a switch statement, what will happen if a break statement is omitted?
Answer:
If a break statement was not placed at the end of a particular case portion? It will move on to the next case portion, possibly causing incorrect output.
Question 150.
Describe how arrays can be passed to a user-defined function
Answer:
One thing to note is that you cannot pass the entire array to a function. Instead, you pass to it a pointer that will point to the array first element in memory. To do this, you indicate the name of the array without the brackets.
Question 151.
What are different storage class specifiers in C?
Answer:
auto, register, static, extern
Question 152.
What is scope of a variable? How are variables scoped in C?
Answer:
Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped.
Question 153.
When should we use pointers in a C program?
Answer:
- To get address of a variable
- For achieving pass-by reference in C: Pointers allow different functions to share and modify their local variables.
- To pass large structures so that complete copy of the structure can be avoided.C
- To implement “linked” data structures like linked lists and binary trees.
Question 154.
What is NULL pointer?
Answer:
NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
Question 155.
What is Dangling pointer?
Answer:
Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.
// EXAMPLE 1
int *ptr = (int *)malloc(sizeof(int)); ....................... ...................... free(ptr); // ptr is a dangling pointer now and operations like following are invalid *ptr = 10; // or printf(“%d”, *ptr);
// EXAMPLE 2
int *ptr = NULL { int x =10; ptr = &x; } // x goes out of scope and memory allocated to x is free now. // So ptr is a dangling pointer now.
Question 156.
What is memory leak? Why it should be avoided
Answer:
Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
/* Function with memory leak */ #include <stdlib.h> void f( ){ int *ptr = (int *) malloc(sizeof(int)); /* Do some work */ return; /* Return without freeing ptr*/ }
Question 157.
What are pointers?
Answer:
Pointers point to specific areas in the memory. Pointers contain the address of a variable, which in turn may contain a value or even an address to another memory.
Question 158.
What is an lvalue?
Answer:
An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant.
Question 159.
What is a pointer value and address?
Answer:
A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.
Question 160.
What is wild pointer?
Answer:
Wild pointers are also known as dangling pointers. Pointers that do not point to a valid object of the appropriate type, or to a distinguished null pointers value in language which support this.
Question 161.
What is the drawback of Pointer?
Answer:
Since the pointers point to a memory location issues of dangling pointers and wild pointers can occur. Dangling pointers are those pointers when the data at which they are pointed may have been modified or deleted. But still the pointer points to that location.
The pointers that are not initialized lead to wild pointers because they will be pointing to some arbitrary memory.
Also memory leaks occur, if a memory is not released.
Question 162.
What is pointer in C?
Answer:
Pointer is a variable that stores/points the address of another variable. Normal variable stores the value of the variable whereas pointer variable stores the address of the variable.
Syntax: data_type *var_name;
Where, * is used to denote that “p” is pointer variable and not a normal variable.
Question 163.
What is null pointer in C?
Answer:
Null pointer is a pointer which is pointing to nothing. Null pointer points to empty location in memory. Value of null pointer is 0. We can make a pointer to point to null as below,
int *p = NULL;
char *p = NULL;
Question 164.
What is NULL in C?
Answer:
NULLisamacrowhichisdefinedinCheaderfiles.ThevalueofNULLmacroisO. It is defined in C header files as below.
#define NULL (void *) 0;
NULL is used for pointers only as it is defined as (void *) 0. It should not be used other than pointers. If NULL is assigned to a pointer, then pointer is pointing to nothing.
Question 165.
What is void pointer in C?
Answer:
- A void pointer is a generic pointer that can be used to point another variable of any data type.
- A void pointer can store the address of a variable belonging to any of the data type. So, void pointer is also called a general-purpose pointer.
Note: int pointer can be used to point a variable of int data type and char pointer can be used to point a variable of char data type.
Question 166.
What is dangling pointer in C?
Answer:
When a pointer is pointing to non-existing memory location is called dangling pointer.
Question 167.
What is wild pointer in C?
Answer:
Uninitialized pointers are called as wild pointers in C which points to arbitrary (random) memory location. This wild pointer may lead a program to behave wrongly or to crash.
Question 168.
What is file pointer in C?
Answer:
- File pointer is a pointer which is used to handle and keep track on the files being accessed. A new data type called “FILE” is used to declare file pointer. This data type is defined in stdio.h file. File pointer is declared as FILE *fp. Where, ‘fp’ is a file pointer.
- fopen( ) function is used to open a file that returns a FILE pointer. Once file is opened, file pointer can be used to perform I/O operations on the file. fclose( ) function is used to close the file.
Question 169.
When can void pointer and null pointer be used in C?
Answer:
Void pointer is a generic pointer that can be used to point another variable of any data type. Null pointer is a pointer which is pointing to nothing.
Question 170.
What is const pointer in C?
Answer:
Const pointer is a pointer that can’t change the address of the variable that is pointing to. Once const pointer is made to point one variable, we can’t change this pointer to point to any other variable. This pointer is called const pointer.
Question 171.
Is pointer arithmetic a valid one? Which arithmetic operation is not valid in pointer? Why?
Answer:
Pointer arithmetic is not valid one. Pointer addition, multiplication and division are not allowed as these are not making any sense in pointer arithmetic. But, two pointers can be subtracted to know how many elements are available between these two pointers.
Question 172.
Is void pointer arithmetic a valid one? Why?
Answer:
Arithmetic operation on void pointer is not valid one. Void pointer is a generic pointer. It is not referring int, char or any other data type specifically. So, we need to cast void pointer to specific type before applying arithmetic operations. Note:
- Pointer arithmetic itself is not valid one. Pointer addition, multiplication and division are not allowed as these are not making any sense in pointer arithmetic.
- But, two pointers can be subtracted to know how many elements are available between these two pointers.
Question 173.
What is the difference between null and zero?
Answer:
NULLis a macro that is defined in C header files. The value ofNULLmacroisO. It is defined in C header files as below, define NULL (void *) 0;
- NULL is used for pointers only as it is defined as (void *) 0. It should not be used other than pointers. If NULL is assigned to a pointer, then the pointer is pointing to nothing.
- 0 (zero) is a value.
Question 174.
What is the difference between null pointer and uninitialized pointer in C?
Answer:
Null pointer is a pointer which is pointing to nothing. Null pointer points to empty location in memory. Value of null pointer is 0. We can make a pointer to point to null as below,
int *p = NULL;
char *p = NULL;
Uninitialized pointers are called as wild pointers in C which points to arbitrary (random) memory location. This wild pointer may lead a program to behave wrongly or to crash.
Question 175.
Can array size be declared at run time?
Answer:
Array size can’t be declared at run time. Size of array must be known during compilation time. So, array size should be declared before compilation.
- Correct example: char array [10];
- Wrong example: char array[i];
- Always, the Array subscript should be positive and it should not be either negative or any variable name.
- If we really don’t know the size of an array, we can use dynamic memory allocation concepts in C which uses malloc( ), calloc( ) functions to allocate memory during execution time.
Question 176.
What are local static variables? What is their use?
Answer:
A local static variable is a variable whose life doesn’t end with a function call where it is declared. It extends for the lifetime of a complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value is 0. For example, the following program prints “0 1″
#include <stdio.h> void fun( ){ // static variables get the default value as 0. static int x; printf(“%d x); x = x + 1; } int main( ){ fun( ); fun( ); return 0; }
// Output: 0 1
Question 177.
What are static functions? What is their use?
Answer:
In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
Question 178.
Reallocating memory
Answer:
When reallocating memory if any other pointers point into same piece of memory do you have to readjust these pointers or do they get readjusted automatically?
Question 179.
What is the difference between Call by Value and Call by Reference?
Answer:
When using Call by Value, you are sending the value of a variable as parameter to a function, whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value in the parameter is not affected by whatever operation that takes place, while in the case of Call by Reference, values can be affected by the process within the function.
Question 180.
What are the actual arguments?
Answer:
When you create and use functions that need to perform an action on some given values, you need to pass these given values to that function. The values that are being passed into the called function are referred to as actual arguments.
Question 181.
What are formal parameters?
Answer:
In using functions in a C program, formal parameters contain the values that were passed by the calling function. The values are substituted in these formal parameters and used in whatever operations as indicated within the main body of the called function. .
Question 182.
Can you pass an entire structure to functions?
Answer:
Yes, it is possible to pass an entire structure to a function in a call by method style. However, some programmers prefer declaring the structure globally, and then pass a variable of that structure type to a function. This method helps maintain consistency and uniformity in terms of argument type.
Question 183.
Is there a built-in function in C that can be used for sorting data?
Answer:
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.
Question 184.
How do you convert strings to numbers in C?
Answer:
You can write you own functions to do string to number conversions, or instead use C’s built-in functions. You can use atof to convert to a floating-point value, atoi to convert to an integer value, and atol to convert to a long integer value.
Question 185.
How many arguments can be passed to a function in C?
Answer:
- Any number of arguments can be passed to a function. There is no limit on this. Function arguments are stored in stack memory rather than heap memory. Stack memory allocation is depending on the operating system.
- So, any number of arguments can be passed to a function as much as stack has enough memory. Program may crash when stack overflows.
Question 186.
What is a static function in C?
Answer:
All functions are global by default in a C program/file. But, static keyword makes a function as a local function which can be accessed only by the program/ file where it is declared and defined. Other programs/files can’t access these static functions.
Question 187.
What is the difference between memcpy( ) & strcpy( ) functions in C?
Answer:
• memcpy( ) function is used to copy a specified number of bytes from one memory to another. Whereas, strcpy( ) function is used to copy the contents of one string into another string.
• memcpy( ) function acts on memory rather than value. Whereas, strcpy( ) function acts on value rather than memory.
Question 188.
What is the difference between memcpy( ) & memmove( ) functions in C?
Answer:
- memcpy( ) function is used to copy a specified number of bytes from one memory to another.
- memmove( ) function is used to copy a specified number of bytes from one memory to another or to overlap on the same memory.
- Difference between memmove( ) and memcpy( ) is, overlap can happen on memmove( ). Whereas, memory overlap won’t happen in memcpy( ) and it should be done in non-destructive way.
Question 189.
Is there any inbuilt library function in C to remove leading and trailing spaces from a string? How will you remove them in C?
Answer:
• There is no inbuilt function to remove leading and trailing spaces from a string in C. We need to write our own function to remove them.
• We need to check the first non-space character in a given string. Then, we can copy that string from where the nonspace character is found. Then, we can check whether any spaces are available in copied string from the end of the string. If space is found, we can copy ‘\0’ in that space until any character is found. Because ‘indicates the end of the string. Now, we have removed leading and trailing spaces in a given string.
Question 190.
What is the difference between strcpy( ) & strncpy( ) functions in C?
Answer:
- strcpy( ) function copies the whole content of one string into another string. Whereas, strncpy( ) function copies portion of contents of one string into another string.
- If the destination string length is less than the source string, the entire/specified source string value won’t be copied into the destination string in both cases.
Question 191.
If you want to execute C program even after main function is terminated, which function can be used?
Answer:
- “atexit( )” function can be used if we want to execute any function after program is terminated normally.
- Syntax: int atexit (void ( func)(void));
- The function “func” will be called automatically without any arguments once the program is terminated normally.
Question 192.
What is exit() function in C?
Answer:
- exit( ) function terminates the program/process normally and returns the status code to the parent program/process.
- Syntax: void exit(int status)
Question 195.
What is the use of “#define” in C?
Answer:
#define is a pre-processor directive that is used to define constant value. This constant can be any of the basic data types.
Question 193.
Is it possible to call exit( ) function more than once in a C program?
Answer:
Yes. We can call atexit( ) function more than once. But, they will be executed in reverse order as a stack.
Example program:
#include <stdio.h> #inc1ude <std1ib.h> void Exitl (void){ printf (“Exitl function is called\n”); } void Exit2 (void){ printf (“Exit2 function is called \n”); } int main (void){ atexit (Exitl); atexit (Exit2); printf (“This is the end of this program.\n”); return 0; }
Question 194.
What is the difference between exit( ) and return( ) in C?
Answer:
- exit( ) is a system call that terminates the current process. exit( ) is not an instruction of C language.
- Whereas return( ) is a C language instruction/statement and it returns from the current function (i.e. provides exit status to calling function and provides control back to the calling function).
Question 195.
What is the use of “#define” in C?
Answer:
#define is a preprocessor directive that is used to define constant value. This constant can be any of the basic data types.
Question 196.
What is the syntax for comments in C?
Answer:
Below is the syntax for comments in C. The characters or words or anything which are given between “/*” and won’t be considered by C compiler for compilation process. These will be ignored by the C compiler during compilation.
Syntax: /* your comments here */
Question 197.
What is a stack?
Answer:
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.
Question 198.
What is a linked list?
Answer:
A linked list is composed of nodes that are connected with one another. In C programming, linked lists are created using pointers. Using linked lists is one efficient way of utilizing memory for storage.
Question 199.
What is FIFO?
Answer:
In C programming, there is a data structure known as queue. In this structure, data is stored and accessed using FIFO format, or First-In-First-Out. A queue represents a fine wherein the first data that was stored will be the first one that is accessible as well.
Question 200.
What are binary trees?
Answer:
Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one. Each side can further branch to form additional nodes, which each node having two pointers as well.
Question 201.
What are the advantages and disadvantages of a heap?
Answer:
Storing data on the heap is slower than it would take when using the stack. However, the main advantage of using the heap is its flexibility. That’s because the memory in this structure can be allocated and remove in any particular order. Slowness in the heap can be compensated if an algorithm was well designed and implemented.
Question 202.
What are static memory allocation and dynamic memory allocation?
Answer:
• Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of the operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable has static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.
• Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.
Question 203.
What is the difference between “calloc” and “malloc”?
Answer:
calloc and malloc are used for dynamic memory allocation. Calloc initializes the memory locations to zero by default but malloc memory contains garbage values.
Question 204.
Where is the function declared as static stored in memory?
Answer:
The usage of static with a function or variable restricts their scope. Is this behavior memory related?
Yes static keyword would not affect where the function gets stored, even if it is static a function will always be stored in stack! But it hides the function from being used in other files other than in which it is declared.
Question 205.
What is pointer linking with the operating system?
Answer:
When we declare a null pointer it addresses the 0 address which is the address of the operating system so we cannot use that address this is what the pointer is linked to OS.
Question 206.
Difference between program memory, data memory, stack memory, and heap memory?
Answer:
When the program is compiled and linked different parts of the program is organized in separate segments. That is our code will be in one segment code means the instructions to be executed this is called as code segment or program memory this is usually read-only. Then there are data which on which the code operates, these data get stored in a segment called data segment. Stack memory is a part of programs memory which will be used as stack in case of function calls to store the IP and parameters variables of the current function.
The three types of memory specified above are owned by the corresponding process or program the linker will give info about where to store which data to the loader, based on these infos loader will load the corresponding image i.e. executable in the memory. Heap memory is the memory that is not owned by the process. OS has set of pages of memory which can be allocated to the process when a process requests for extra memory, these requests are done by the malloc and calloc function calls.
This memory is allocated when the program is running and according to the current condition of the process i.e. allocated dynamically that’s why this is called as dynamic allocation.
I have used words program and process invariably. Because a program needs physical primary memory when it gets executed and a program under execution is a process, Hope this explanation is clear.
Question 207.
C is a structural or high-level or middle-level language which one is the correct answer
Answer:
C is a middle-level language.
Because this language contains both the features of both high-level language and low-level languages. Also can be called a structured programming language.
Question 208.
What do the characters “r” and “w” mean when writing programs that will make use of files?
Answer:
“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.
Question 209.
What is the difference between text files and binary files?
Answer:
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 Is and Os that only computers can interpret.
Question 210.
How do you search data in a data file using a random access method?
Answer:
Use the fseek( ) function to perform random access input/output 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.
Question 211.
Difference between “printf” and “sprintf”.
Answer:
printf will write to the console, sprintf will write to the buffer
Question 212.
Difference between printf and cprintf
Answer:
printf outputs to the standard output stream (stdout) fprintf goes to a file handle (FILE*) sprintf goes to a buffer you allocated, (char*)
Question 213.
Garbage Value
Answer:
In C if a variable is not assigned a value then why does it take garbage value? •
Question 214.
What are preprocessor directives?
Answer:
Preprocessor directives are placed at the beginning of every C program. This is where library files are specified, which would depend on what functions are to be used in the program. Another use of preprocessor directives is the declaration of constants. Preprocessor directives begin with the # symbol.
Question 215.
What is Macro? Why do we use a macro?
Answer:
- Macro is a name that is given to a value or to a piece of code/block in a program. Instead of using the value, we can use a macro that will replace the value in a program.
- The reason for using a macro is, consider the below example.
- You are using a person’s age as 50 in many places in your program. If you use direct value in all the places in your program, it is very difficult to change the age value in the future if you want to change it to 60. If you use a macro in your program, it is very simple to replace the value and we can change in only one place which will change the value in all places in your program.
Syntax: #define <MACR0 _NAME> VALUE
Example: Original declaration — #define AGE 50
Modified declaration – #define AGE 60
Question 216.
What is the “##” operator in C?
Answer:
## is a pre-processor macro in C. It is used to concatenate 2 tokens into one token.
Example program:
#include<stdio.h> #define concatination(a,b) a ## b int main ( ){ int ab = 1000; printf(“The concatenated value is:%d \n”,concatination(a,b)); return 0; }
Output: The concatenated value is: 1000
Question 217.
How will you override an existing macro in C?
Answer:
To override an existing macro, we need to undefine the existing macro using “#undef’. Then, we need to define the same macro again with the new value.
Question 218.
What is pragma in C? Or how will you execute functions before and after main function in C program?
Answer:
#pragma is a. pre-processor macro in C. It is used to call a function before and after main function in a C program.
Example program:
#include <stdio.h> void functionlC ); void function2( ); #pragma startup function1 #pragma exit function2 int main( ){ printf C “\n Now we are in main function” ) ; return 0; } void functionl( ){ printf(“\nFunctionl is called before main function call”); } void function2( ){ printf C “\nFunction2 is called just before end of “ \ «main function” ) }
Output:
Function1 is called before the main function call
Now we are in the main function
Function2 is called just before the end of the main function
Question 219.
How to check whether the macro is defined or not in a C program?
Answer:
“#ifdef ’ pre-processor directive checks whether a particular macro is defined or not in a C program.
Question 220.
What is the difference between typedef & Macros?
Answer:
Typedef is used to create a new name to an already existing data type. Redefine the name that creates conflict with the previous declaration,
eg: typedef unsigned int UINT32
Macros [#define] is a direct substitution of the text before completing the whole code. In the given example, it’s just a textual substitution. Where there is a possibility of redefining the macro
eg: #define pointer char *
under chpointer
#define chPointer int *
Question 221.
What are pre-processor directives?
Answer:
- Pre-processor directives are placed at the beginning of a C program. They begin with the # symbol.
- This is the place, where library files are specified depending on the functions to be used in the program.
- Pre-processor directives are also used for the declaration of constants.
Question 222.
What is the difference between structure & union?
Answer:
Differences are:
- union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total i memory required by the members.
- In a union, one block is used by all the members of the union but in the case of structure, each member has their own memory space
- union is best in the environment where memory is less as it shares the memory allocated. But structure cannot implement in shared memory.
- As memory is shared, ambiguity is more in the union, but less in structure.
- The self-referential union cannot be implemented in any data structure, but the self referential structure can be implemented.
Question 223.
What is wrong with this statement? myName = “Robin”;
Answer:
You cannot use the = sign to assign values to a string variable. Instead, use the strcpy function. The correct statement would be: strcpy(myName, “Robin”);
Question 224.
How do you determine the length of a string value that was stored in a variable?
Answer:
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
i character length of the string value.
Question 225.
How do you declare a variable that will hold string values?
Answer:
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.
Question 226.
Can array subscripts have a negative value in C?
Answer:
No. Array subscripts should not have a negative value. Always, it should be positive.
Question 227.
What is the difference between array and string in C?
Answer:
- The array can hold any of the data types. But, the string can hold only char data type.
- Array size must be a constant value. So, array size can’t be changed once declared. But, string size can be modified using a char pointer.
- The array is not ended with a null character by default. But, the string is ended with a null (‘\0’) character by default.
Question 228.
What is meant by core dump in C?
Answer:
- Core dump or core is a file, generated when a program is crashed or terminated abnormally because of segmentation fault or some other reason. Information of the memory used by a process is dumped in a file called core. This file is used for debugging purposes. Core dump has file names like “core.<process_id>”
- The core dump file is created in the current working directory when a process terminates abnormally. A core dump is a typical error that occurs because of illegal memory access.
- A core dump is also called a memory dump, storage dump, or dump.
Question 229.
What is the purpose of the “register” keyword?
Answer:
It is used to make the computation faster.
The register keyword tells the compiler to store the variable onto the CPU register if space on the register is available. However, this is a very old technique. Today’s processors are smart enough to assign the registers themselves and hence using the register keyword can actually slow down the operations if the usage is incorrect.
Question 230.
What is the purpose of the “register” keyword?
Answer:
The keyword ‘register’ instructs the compiler to persist the variable that is being declared, in a CPU register.
Ex: register int number;
The persistence of register variables in the CPU register is to optimize the access. Even the optimization is turned off; the register variables are forced to store in the CPU register.
Question 231.
Explain the use of the “auto” keyword.
Answer:
When a certain variable is declared with the keyword ‘auto’ and initialized to a certain value, then within the scope of the variable, it is reinitialized upon being called repeatedly.
Question 232.
Explain the use of the “auto” keyword.
Answer:
The keyword ‘auto’ is used extremely rarely. A variable is set by default to auto. The declarations:
auto int number; and int number;
has the same meaning. The variables of type static, extern and register need to be explicitly declared, as their need is very specific. The variables other than the above three are implied to be auto variables. For better readability, auto keywords can be used.