The document provides a series of medium-level questions and answers related to C, C++, and Java programming. Key topics include memory allocation differences between malloc() and calloc(), the behavior of the static keyword, segmentation faults, pointers versus arrays, and the use of the volatile keyword. Additionally, it includes code examples for string reversal, bit counting, stack implementation, and discusses macros versus functions and the concept of virtual functions.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
15 views
C C++ Java Questions and Answers (1)
The document provides a series of medium-level questions and answers related to C, C++, and Java programming. Key topics include memory allocation differences between malloc() and calloc(), the behavior of the static keyword, segmentation faults, pointers versus arrays, and the use of the volatile keyword. Additionally, it includes code examples for string reversal, bit counting, stack implementation, and discusses macros versus functions and the concept of virtual functions.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
C, C++, and Java: Medium Level Questions and Answers
Explain the difference between malloc() and calloc().
malloc() allocates uninitialized memory, while calloc() allocates zero-initialized memory.
How does the static keyword behave in C? Provide an example.
The static keyword in C limits a variable's scope to the file or function it is declared in. It also retains its value between function calls.
Write a program to reverse a string without using library functions.
Code: ```c void reverseString(char* str) { int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } } ```
Explain what a segmentation fault is and when it can occur.
A segmentation fault occurs when a program tries to access memory that it's not allowed to. It often results from dereferencing invalid pointers.
How does a pointer differ from an array in memory access?
An array refers to a contiguous memory block, while a pointer is a variable that stores the address of any memory location.
What is the purpose of the volatile keyword?
The volatile keyword prevents the compiler from optimizing code involving variables that can be modified outside the program's flow.
Write a program to count the number of bits set to 1 in an integer.
Code: ```c int countBits(int num) { int count = 0; while (num) { count += num & 1; num >>= 1; } return count; } ```
Explain the difference between ++i and i++.
++i increments the value before it's used in an expression, while i++ increments the value after it's used.
What are the pros and cons of using macros instead of functions? Macros are faster since they are expanded inline but lack type safety and debugging support.
Write a program to implement a basic stack using arrays.
Code: ```c #define SIZE 100 int stack[SIZE], top = -1; void push(int val) { if (top < SIZE - 1) stack[++top] = val; } int pop() { return (top >= 0) ? stack[top--] : -1; } ```
Explain the concept of virtual functions and their use case.
Virtual functions allow runtime polymorphism, letting derived classes override base class methods.