0% found this document useful (0 votes)
60 views16 pages

Campus Monk C

some thing useful

Uploaded by

ashwani verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views16 pages

Campus Monk C

some thing useful

Uploaded by

ashwani verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Pdf 4 - C based Questions

Q1. Why is C called a mid-level programming language?

Answer:
C has characteristics of both assembly-level i.e. low-level and higher-level languages. So
as a result, C is commonly called a middle-level language. Using C, a user can write an
operating system as well as create a menu-driven consumer billing system.

Q2. What are the features of the C language?

Answer:
1. It is Simple And E cient.
2. C language is portable or Machine Independent.
3. C is a mid-level Programming Language.
4. It is a structured Programming Language.
5. It has a function-rich library.
6. Dynamic Memory Management.
7. C is super fast.
8. We can use pointers in C.
9. It is extensible.

Q3. What is a token?

Answer:
The individual elements of a program are called Tokens. There are following 6 types of
tokens are available in C:
• Identi ers
• Keywords
• Constants
• Operators
• Special Characters
• Strings

Q4. What is the use of printf() and scanf() functions?

Answer:
• printf() is used to print the output on the display.
• scanf() is used to read formatted data from the keyboard.

Q5. What's the value of the expression 5["abxdef"]?

Answer:
The string mentioned "abxdef" is an array, and the expression is equal to "abxdef"[5].
The answer is 'f'.
fi
ffi
Q6. What is a built-in function in C?

Answer:
Built-function is also known as library functions that are provided by the system to make
the life of a developer easy by assisting them to do certain commonly used prede ned
tasks. For example, if you need to print output or your program into the terminal, we use
printf() in C. The most commonly used built-in functions in C are scanf(), printf(), strcpy,
strlwr, strcmp, strlen, strcat, etc.

Q7. What is a Preprocessor?

Answer:
A preprocessor is a software program that processes a source le before sending it to be
compiled. Inclusion of header les, macro expansions, conditional compilation, and line
control are all possible with the preprocessor.

Q8. In C, What is the #line used for?

Answer:
#line is used as a preprocessor to re-set the line number in the code, which takes a
parameter as line number. Here is an example for the same.

Q9. How can a string be converted to a number?

Answer:
The function takes the string as an input that needs to be converted to an integer.
Syntax: int atoi(const char *string)

Return Value:
• On successful conversion, it returns the desired integer value
• If the string starts with alpha-numeric char or only contains alpha-num char, 0 is
returned.
• In case string starts with numeric character but is followed by alpha-num char, the
string is converted to integer till the rst occurrence of alphanumeric char.

Q10. How can a number be converted to a string?

Answer:
The function takes a pointer to an array of char elements that need to be converted, and a
format string needs to be written in a bu er as a string
Syntax: int sprintf(char *str, const char *format, ...)

Q11. What is recursion in C?

Answer:
fi
fi
ff
fi
fi
When a function in C calls a copy of itself, this is known as recursion. To put it another
way, when a function calls itself, this technique is called Recursion. Also, this function is
known as recursive function.

Q12. Why doesn’t C support function overloading?

Answer:
After you compile the C source, the symbol names need to be intact in the object code. If
we introduce function overloading in our source, we should also provide name mangling
as a preventive measure to avoid function name clashes. Also, as C is not a strictly typed
language many things(ex: data types) are convertible to each other in C. Therefore, the
complexity of overload resolution can introduce confusion

Q13. What is the di erence between global int and static int declaration?

Answer:
The di erence between this is in scope. A truly global variable has a global scope and is
visible everywhere in your program.global_temp is a global variable that is visible to
everything in your program, although to make it visible in other modules, you'd need an ”
extern int global_temp; ” in other source les if you have a multi- le project.

A static variable has a local scope but its variables are not allocated in the stack segment
of the memory. It can have less than global scope, although - like global variables - it
resides in the .bss segment of your compiled binary.

Q14. What is a pointer in C?

Answer:
A pointer is a variable that stores or points to another variable's address. The value of a
variable is stored in a normal variable, whereas the address of a variable is stored in a
pointer variable.

Q15. Di erence between const char* p and char const* p?

Answer:
• const char* p is a pointer to a const char.
• char const* p is a pointer to a char const.
Since const char and char const are the same, it's the same.

Q16. What is pointer to pointer in C?

Answer:
In C, a pointer can also be used to store the address of another pointer. A double pointer
or pointer to pointer is such a pointer. The address of a variable is stored in the rst
pointer, whereas the address of the rst pointer is stored in the second pointer.

Syntax: int **p; // pointer to a pointer which is pointing to an integer


ff
ff
ff
fi
fi
fi
fi
Q17. Why n++ executes faster than n+1 ?

Answer:
n++ being a unary operation, it just needs one variable. Whereas, n = n + 1 is a binary
operation that adds overhead to take more time (also binary operation: n += 1). However,
in modern platforms, it depends on few things such as processor architecture, C
compiler, usage in your code, and other factors such as hardware problems. While in the
modern compiler even if you write n = n + 1 it will get converted into n++ when it goes
into the optimized binary, and it will be equivalently e cient.

Q18. What is typecasting in C?

Answer:
Typecasting is the process to convert a variable from one datatype to another. If we want
to store the large type value to an int type, then we will convert the data type into another
data type explicitly.

Syntax: (data_type)expression;

Q19. What are the advantages of Macro over function?

Answer:
Macro on a high-level copy-paste, its de nitions to places wherever it is called. Due to
which it saves a lot of time, as no time is spent while passing the control to a new
function and the control is always with the callee function. However, one downside is the
size of the compiled binary is large but once compiled the program comparatively runs
faster.

Q20. What are Enumerations?

Answer:
Enumeration, also known as Enum in C, is a user-de ned data type. It consists of
constant integrals or integers that have names assigned to them by the user. Because the
integer values are named with enum in C, the whole program is simple to learn,
understand, and maintain by the same or even di erent programmer.

Q21. When should we use the register storage speci er?

Answer:
If a variable is used frequently, it should be declared with the register storage speci er,
and the compiler may allocate a CPU register for its storage to speed up variable lookup.

Q22. Specify di erent types of decision control statements?


ff
fi
ff
fi
ffi
fi
fi
Answer:
All statements written in a program are executed from top to bottom one by one. Control
statements are used to execute/transfer the control from one part of the program to
another depending on the condition.
• If-else statement.
• normal if-else statement.
• Else-if statement
• nested if-else statement.
• Switch statement.

Q23. What is an r-value and l-value?

Answer:
• The term "r-value" refers to a data value stored in memory at a given address. An r-
value is an expression that cannot have a value assigned to it, hence it can only
exist on the right side of an assignment operator(=).
• The term "l-value" refers to a memory location that is used to identify an object.
The l-value can be found on either the left or right side of an assignment
operator(=). l-value is frequently used as an identi er.

Q24. What is the di erence between malloc() and calloc()?

Answer:
calloc() and malloc() are memory dynamic memory allocating functions. The main
di erence is that malloc() only takes one argument, which is the number of bytes, but
calloc() takes two arguments, which are the number of blocks and the size of each block.

Q25. What is the di erence between struct and union in C?

Answer:
A struct is a group of complex data structures stored in a block of memory where each
member on the block gets a separate memory location to make them accessible at once

Whereas in the union, all the member variables are stored at the same location on the
memory as a result to which while assigning a value to a member variable will change the
value of all other members.

Q26. What is call by reference in functions?

Answer:
When we caller function makes a function call bypassing the addresses of actual
parameters being passed, then this is called call by reference. In call by reference, the
operation performed on formal parameters a ects the value of actual parameters
because all the operations performed on the value stored in the address of actual
parameters.
ff
ff
ff
ff
fi
Q27. What is pass by reference in functions?

Answer:
In Pass by reference, the callee receives the address and makes a copy of the address of
an argument into the formal parameter. Callee function uses the address to access the
actual argument (to do some manipulation). If the callee function changes the value
addressed at the passed address it will be visible to the caller function as well.

Q28. What is a memory leak? How to avoid it?

Answer:
When we assign a variable it takes space of our RAM (either heap or RAM)dependent on
the size of data type, however, if a programmer uses a memory available on the heap and
forgets to a delta it, at some point all the memory available on the ram will be occupied
with no memory left this can lead to a memory leak.

To avoid memory leaks, you can trace all your memory allocations and think forward,
where you want to destroy (in a good sense) that memory and place delete there.

Q29. What is Dynamic memory allocation in C? Name the dynamic allocation


functions.

Answer:
C is a language known for its low-level control over the memory allocation of variables in
DMA there are two major standard library malloc() and free.

The malloc() function takes a single input parameter which tells the size of the memory
requested It returns a pointer to the allocated memory. If the allocation fails, it returns
NULL. The prototype for the standard library function is like this:
void *malloc(size_t size);

The free() function takes the pointer returned by malloc() and de-allocates the memory. No
indication of success or failure is returned. The function prototype is like this:
void free(void *pointer);

There are 4 library functions provided by C de ned under <stdlib.h> header le to


facilitate dynamic memory allocation in C programming. They are:
• malloc()
• calloc()
• free()
• realloc()

Q30. What is typedef?

Answer:
fi
fi
typedef is a C keyword, used to de ne alias/synonyms for an existing type in C language.
In most cases, we use typedef's to simplify the existing type declaration syntax. Or to
provide speci c descriptive names to a type.

typedef provides an alias name to the existing complex type de nition. With typedef, you
can simply create an alias for any type. Whether it is a simple integer to complex function
pointer or structure declaration, typedef will shorten your code.

Syntax: typedef <existing-type> <new-type-identi ers>;

Q31. Why is it usually a bad idea to use gets()? Suggest a workaround.

Answer:
The standard input library gets() reads user input till it encounters a new line character.
However, it does not check on the size of the variable being provided by the user is under
the maximum size of the data type which makes the system vulnerable to bu er over ow
and the input being written into memory where it isn’t supposed to.
We, therefore, use gets() to achieve the same with a restricted range of input.

Q32. What is the di erence between #include "..." and #include <...>?

Answer:
In practice, the di erence is in the location where the preprocessor searches for the
included le.

For #include < lename> the C preprocessor looks for the lename in the prede ned list of
system directories rst and then to the directories told by the user(we can use -I option to
add directories to the mentioned prede ned list).

For #include " lename" the preprocessor searches rst in the same directory as the le
containing the directive, and then follows the search path used for the #include
< lename> form. This method is normally used to include programmer-de ned header
les.

Q33. What are dangling pointers? How are dangling pointers di erent from memory
leaks?

Answer:
The dangling pointer points to a memory that has already been freed. The storage is no
longer allocated. Trying to access it might cause a Segmentation fault. The dangling
pointer points to a memory that has already been freed. The storage is no longer
allocated. Trying to access it might cause a Segmentation fault.

A memory leak is something where the memory allocated is not freed which causes the
program to use an unde ned amount of memory from the ram making it unavailable for
every other running program(or daemon) which causes the programs to crash. There are
fi
fi
fi
fi
fi
fi
ff
fi
ff
fi
fi
fi
fi
fi
fi
fi
ff
fi
ff
fi
fi
fl
various tools like O pro le testing which is useful to detect memory leaks on your
programs.

Q34. What is the di erence between ‘g’ and “g” in C?

Answer:
In C double-quotes variables are identi ed as a string whereas single-quoted variables
are identi ed as the character. Another major di erence being the string (double-quoted)
variables end with a null terminator that makes it a 2 character array.

Q35. What is a near pointer and a far pointer in C?

Answer:
• Near Pointer: In general, the near pointer can be considered because it is used to
hold the address, which has a maximum size of just 16 bits. We can't store an
address with a size larger than 16 bits using the near pointer. All other smaller
addresses that are within the 16-bit limit, on the other hand, can be stored.
Because we can only access 64kb of data at a time, you might assume the 16 bits
are insu cient. As a result, it is regarded as one of the near-pointer's biggest
drawbacks, which is why it is no longer commonly used.
• Far Pointer: A far pointer is considered a pointer of size 32 bits. It can, however,
use the current segment to access information stored outside the computer's
memory. Although, in order to use this type of pointer, we usually need to allocate
the sector register to store the data address in the current segment.

Q36. Which structure is used to link the program and the operating system?

Answer:
The le structure is used to link the operating system and a program. The header le
"stdio.h" (standard input/output header le) de nes the le. It contains information about
the le being used like its current size and its memory location. It contains a character
pointer that points to the character which is currently being opened. When you open a
le, it establishes a link between the program and the operating system about which le is
to be accessed.

Q37. Suppose a global variable and local variable have the same name. Is it possible
to access a global variable from a block where local variables are de ned?

Answer:
No. This isn’t possible in C. It’s always the most local variable that gets preference.

Q38. Which is better #de ne or enum?


fi
fi
fi
ffi
fi
ff
fi
fi
fi
fi
fi
ff
fi
fi
fi
fi
• If we let it, the compiler can build enum values automatically. However, each of the
de ned values must be given separately.
• Because macros are preprocessors, unlike enums, which are compile-time entities,
the source code is unaware of these macros. So, if we use a debugger to debug
the code, the enum is superior.
• Some compilers will give a warning if we use enum values in a switch and the
default case is missing.
• Enum always generates int-type identi ers. The macro, on the other hand, allowed
us to pick between various integral types.
• Unlike enum, the macro does not have a de ned scope constraint.

Q39. Can we compile a program without a main() function?

Answer:
Yes, we can compile a program without main() function Using Macro

Q40. How do you override a de ned macro?

Answer:
To override a de ned macro we can use #ifdef and #undef preprocessors as follows:
• #ifdef A
• #undef A
• #endif
• #de ne A 10
If macro A is de ned, it will be unde ned using undef and then de ned again using de ne.

Q41. How to call a function before main()?

Answer:
To call a function before the main(), pragma startup directive should be used.
Example:
#pragma startup fun
void fun()
{
printf("In fun\n");
}
main()
{
printf("In main\n");
}

This pragma directive, on the other hand, is compiler-dependent. This is not supported by
gcc. As a result, it will ignore the startup directive and produce no error. But the output, in
that case, will be In main.

Q42. Di erentiate between the macros and the functions.


fi
fi
ff
fi
fi
fi
fi
fi
fi
fi
fi
Answer:
Macros Functions
It is It is compiled
preprocessed not
rather than preprocessed.
compiled.
It is Function checks
preprocessed for compilation
rather than errors.
compiled.
Code length is Code length
increased. remains the
same.
Macros are Functions are a
faster in bit slower in
execution. execution.
Macros are Functions are
useful when a helpful when a
small piece of large piece of
code is used code is
multiple times in repeated a
a program. number of
times.

Q43. Di erentiate Source Codes from Object Codes

Answer:
The di erence between the Source Code and Object Code is that Source Code is a
collection of computer instructions written using a human-readable programming
language while Object Code is a sequence of statements in machine language, and is the
output after the compiler or an assembler converts the Source Code.
The lastly, Object Code is the way the changes are re ected. When the Source Code is
modi ed, each time the Source Code needs to be compiled to re ect the changes in the
Object Code.

Q44. What are header les and what are its uses in C programming?

Answer:
In C header les must have the extension as .h, which contains function de nitions, data
type de nitions, macro, etc. The header is useful to import the above de nitions to the
source code using the #include directive. For example, if your source code needs to take
input from the user do some manipulation and print the output on the terminal, it should
have stdio.h le included as #include <stdio.h>, with which we can take input using
scanf() do some manipulation and print using printf().
fi
ff
fi
ff
fi
fi
fi
fl
fl
fi
fi
Q45. When is the "void" keyword used in a function?

Answer:
The keyword “void” is a data type that literally represents no data at all. The most obvious
use of this is a function that returns nothing.The other use for the void keyword is a void
pointer. A void pointer points to the memory location where the data type is unde ned at
the time of variable de nition. Even you can de ne a function of return type void* or void
pointer meaning “at compile time we don’t know what it will return.

Q46. What is dynamic data structure?

Answer:
A dynamic data structure (DDS) refers to an organization or collection of data in memory
that has the exibility to grow or shrink in size, enabling a programmer to control exactly
how much memory is utilized. Dynamic data structures change in size by having unused
memory allocated or de-allocated from the heap as needed.
Dynamic data structures play a key role in programming languages like C, C++, and Java
because they provide the programmer with the exibility to adjust the memory
consumption of software programs.

Q47. What is the use of a static variable in C?

Answer:
• A variable which is declared as static is known as a static variable. The static
variable retains its value between multiple function calls.
• Static variables are used because the scope of the static variable is available in the
entire program. So, we can access a static variable anywhere in the program.
• The static variable is initially initialized to zero. If we update the value of a variable,
then the updated value is assigned.
• The static variable is used as a common value which is shared by all the methods.
• The static variable is initialized only once in the memory heap to reduce the
memory usage.

Q48. What is an auto keyword in C?

Answer:
in C, every local variable of a function is known as an automatic (auto) variable. Variables
which are declared inside the function block are known as a local variable. The local
variables are also known as an auto variable. It is optional to use an auto keyword before
the data type of a variable. If no value is stored in the local variable, then it consists of a
garbage value.

Q49. What is command line argument?

Answer:
fl
fi
fi
fl
fi
The argument passed to the main() function while executing the program is known as
command line argument.
example:
main(int count, char *args[])
{
//code to be executed
}

Q50. What is the di erence between getch() and getche()?

Answer:
The getch() function reads a single character from the keyboard. It doesn't use any bu er,
so entered data will not be displayed on the output screen.
The getche() function reads a single character from the keyword, but data is displayed on
the output screen. Press Alt+f5 to see the entered character.

Q51. What is the acronym for ANSI?

Answer:
The ANSI stands for " American National Standard Institute." It is an organization that
maintains the broad range of disciplines including photographic lm, computer
languages, data encoding, mechanical parts, safety and more.

Q52. What is huge pointers?

Answer:
huge pointers have explicit selector. When you perform pointer arithmetic the selector
can be modi ed.

Q53. What are the functions to open and close the le in C language?

Answer:
The fopen() function is used to open le whereas fclose() is used to close le.

Q54. Can we access the array using a pointer in C language?

Answer:
Yes, by holding the base address of array into a pointer, we can access the array using a
pointer.

Q55. Who is the main contributor in designing the C language after Dennis Ritchie?

Answer:
Brain Kernighan

Q56. What is the use of a semicolon (;) at the end of every program statement?
fi
ff
fi
fi
fi
fi
ff
Answer:
It is majorly related to how the compiler reads( or parses) the entire code and breaks it
into a set of instructions(or statements), to which semicolon in C acts as a boundary
between two sets of instructions.

Q57. Can you tell me how to check whether a linked list is circular?

Answer:
Circular linked list is a variation of a linked list where the last node is pointing to the rst
node's information part. Therefore the last node does not point to null.

Algorithm to nd a circular linked list:

• Traverse the linked list


• Check if the node is pointing to the head.
• If yes then it is circular.

———————————————————

DIY Coding Questions

Q1. How can you remove duplicates in an array? Illustrate with a code.

Q2. Write a program to get the higher and lower nibble of a byte without using shift
operator?

Q3. Write a program to check if it is a palindrome number or not using a recursive


method.

Q4. Write a program to check the given number format is in binary or not.

Q5. Write a Program to nd a sum of digits of a number using recursion.

Q6. Write a program to Add Two Numbers Without Using the Addition Operator and
Subtract Two Number Without Using Subtraction Operator.

Q7. Multiply an Integer Number by 2 Without Using Multiplication Operator.

Q8. Write a programCheck whether the number is EVEN or ODD, without using any
arithmetic or relational operators.

Q9. Write a program to Reverse the Linked List. Input: 1->2->3->4->5->NULL


Output: 5->4->3->2->1->NULL.

Q10. Write a program to print "hello world" without using a semicolon?

Q11. Write a program to print Fibonacci series without using recursion?


fi
fi
fi
Q12. Write a program to swap two numbers without using the third variable?

Q13. Write a program to nd the node at which the intersection of two singly linked
lists begins.

Q14. Write a program to Merge Two sorted Linked List.

Q15. Check for Balanced Parentheses using Stack


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the
input string is valid.
An input string is valid if:
• Open brackets must be closed by the same type of brackets.
• Open brackets must be closed in the correct order.
Example 1:
Input: s = "()"
Output: true

Example 2:
Input: s = "()[]{}"
Output: true

Example 3:
Input: s = "(]"
Output: false

————————————————

TEST YOURSELF

Q1. C variable cannot start with which of the following option

A. An alphabet
B. A number
C. A special symbol other than underscore
D. Both (B) and (C)

Q2. During function call, Default Parameter Passing Mechanism is called as

A. Call by Value
B. Call by reference
C. Call by address
D. Call by name

Q3. The statement printf(“%d”, 10 ? 0 ? 5 : 1 : 12); will print?


fi
A. 10
B. 0
C. 12
D. 1

Q4. In the switch statement, each case instance value must be _______?

A. Constant
B. Variable
C. Special symbol
D. None to the above

Q5. Which is the correct syntax to declare constants in C?

A. int constant var =10;


B. int const var = 10;
C.const int var = 10;
D. Both (B) and (C)

Q6. Bitwise operators can operate upon?

A. double and chars


B. oats and doubles
C. ints and oats
D. ints and chars

Q7. The number of binary trees that can be formed using 5 nodes are

A. 30
B. 42
C. 108
D. 36

Q8. What is the correct syntax to access the value of struct variable book{ price, page }?

A. printf("%d%d", book.price, book.page);


B. printf("%d%d", price.book, page.book);
C. printf("%d%d", price::book, page::book);
D. printf("%d%d", price->book, page->book);

Q9. Which of the following data structures is linear type?

A. String
B. Queue
C. List
D. All of the above
fl
fl
Q10. A binary tree with 27 nodes has _______ null branches.

A. 54
B. 27
C. 26
D. None of the above

————————————

Answer to test yourself:


1. both(B) and (C)
2. Call by value
3. 1
4. Constant
5. Both B & C
6. ints and chars
7. 42
8. printf("%d%d", book.price, book.page);
9. All of the above
10. None of the above

You might also like