C & C++ Interview Questions You'll Most Likely Be Asked
()
About this ebook
C & C++ Interview Questions You'll Most Likely Be Asked is a perfect companion to stand ahead above the rest in today’s competitive job market. Rather than going through comprehensive, textbook-sized reference guides, this book includes only the information required immediately for job search to build an I
Read more from Vibrant Publishers
Operations and Supply Chain Management Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsStakeholder Engagement Essentials You Always Wanted To Know: Self Learning Management Rating: 5 out of 5 stars5/5HR Analytics Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsDigital SAT Reading and Writing Practice Questions: Test Prep Series Rating: 5 out of 5 stars5/5Advanced Java Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 1 out of 5 stars1/5GRE Master Wordlist: 1535 Words for Verbal Mastery: Test Prep Series Rating: 4 out of 5 stars4/5Financial Management Essentials You Always Wanted to Know: 5th Edition: Self Learning Management Rating: 0 out of 5 stars0 ratingsBusiness Strategy Essentials You Always Wanted To Know: Self Learning Management Rating: 5 out of 5 stars5/5Organizational Behavior Essentials You Always Wanted To Know: Self Learning Management Rating: 5 out of 5 stars5/5SAP HANA Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsCore Java Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 4 out of 5 stars4/5Diversity in the Workplace Essentials You Always Wanted To Know: Self Learning Management Rating: 5 out of 5 stars5/5Project Management Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsMicroeconomics Essentials You Always Wanted to Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsFinancial Accounting Essentials You Always Wanted to Know: 5th Edition: Self Learning Management Rating: 0 out of 5 stars0 ratingsGMAT Analytical Writing: Solutions to the Real Argument Topics: Test Prep Series Rating: 4 out of 5 stars4/5SQL Server Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsJava/J2EE Design Patterns Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsHuman Resource Management Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsBusiness Law Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsAdvanced C++ Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsJavaScript Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsC & C++ Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsRestful Java Web Services Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsSAS Programming Guidelines Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsLeadership Essentials You Always Wanted To Know: Self Learning Management Rating: 0 out of 5 stars0 ratingsCCNA Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsData Structures Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsAdvanced SAS Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsPython Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratings
Related to C & C++ Interview Questions You'll Most Likely Be Asked
Titles in the series (33)
SQL Server Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsC# Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsC & C++ Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsAdvanced C++ Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsHadoop BIG DATA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsORACLE PL/SQL Interview Questions You'll Most Likely Be Asked Rating: 5 out of 5 stars5/5Data Structures & Algorithms Interview Questions You'll Most Likely Be Asked Rating: 1 out of 5 stars1/5Software Testing Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsAdvanced JAVA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsHibernate, Spring & Struts Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsJava / J2EE Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsCORE JAVA Interview Questions You'll Most Likely Be Asked Rating: 4 out of 5 stars4/5JSP-Servlet Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsBase SAS Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsPython Interview Questions You'll Most Likely Be Asked Rating: 2 out of 5 stars2/5Automated Software Testing Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsLinux System Administrator Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsAdvanced SAS Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsSAP HANA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsJavaScript Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsCCNA Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsIBM WebSphere Application Server Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsSAS Programming Guidelines Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsOperating Systems Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsSelenium Testing Tools Interview Questions You'll Most Likely Be Asked: Second Edition Rating: 0 out of 5 stars0 ratingsJava/J2EE Design Patterns Interview Questions You'll Most Likely Be Asked: Second Edition Rating: 0 out of 5 stars0 ratingsCloud Computing Interview Questions You'll Most Likely Be Asked: Second Edition Rating: 0 out of 5 stars0 ratingsSAS Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsUNIX Shell Programming Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratings
Related ebooks
Learn Multithreading with Modern C++ Rating: 0 out of 5 stars0 ratingsUpdate to Modern C++ Rating: 0 out of 5 stars0 ratingsOperating Systems Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsC++: A Beginner's Guide, Second Edition Rating: 0 out of 5 stars0 ratingsC Programming Concepts Rating: 0 out of 5 stars0 ratingsFunctional Programming in C++ Rating: 0 out of 5 stars0 ratingsUNIX Shell Programming Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsProgramming Concepts in C++ Rating: 0 out of 5 stars0 ratingsKotlin at a Glance: Use of Lambdas and higher-order functions to write more concise, clean, reusable, and simple code Rating: 0 out of 5 stars0 ratingsLearn Programming by Coding Like a Professional: Create Games, Apps, & Programs Rating: 0 out of 5 stars0 ratingsJava: Tips and Tricks to Programming Code with Java: Java Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsObjective-C Programming Nuts and bolts Rating: 0 out of 5 stars0 ratingsAnalysis and Design of Algorithms: A Beginner’s Hope Rating: 0 out of 5 stars0 ratingsMastering C Pointers: Tools for Programming Power Rating: 2 out of 5 stars2/5Advanced C++ Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsData Structures & Algorithms Interview Questions You'll Most Likely Be Asked Rating: 1 out of 5 stars1/5Beginning Data Structures Using C Rating: 4 out of 5 stars4/5Boost.Asio C++ Network Programming - Second Edition Rating: 0 out of 5 stars0 ratingsStructures and C Rating: 4 out of 5 stars4/5Advanced C Concepts and Programming: First Edition Rating: 3 out of 5 stars3/5LLVM Essentials Rating: 1 out of 5 stars1/5TCP/IP Sockets in Java: Practical Guide for Programmers Rating: 4 out of 5 stars4/5Real-Time Embedded Systems Rating: 0 out of 5 stars0 ratingsData Structures II Essentials Rating: 0 out of 5 stars0 ratingsClean C++20: Sustainable Software Development Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsLLVM Cookbook Rating: 1 out of 5 stars1/5Beginning Linux Programming Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practices to Programming Code with JavaScript: JavaScript Computer Programming, #3 Rating: 0 out of 5 stars0 ratingsC Clearly - Programming With C In Linux and On Raspberry Pi Rating: 0 out of 5 stars0 ratings
Programming For You
Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPython GUI Programming Cookbook - Second Edition Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for C & C++ Interview Questions You'll Most Likely Be Asked
0 ratings0 reviews
Book preview
C & C++ Interview Questions You'll Most Likely Be Asked - Vibrant Publishers
C And C++
Interview Questions
You'll Most Likely Be Asked
Job Interview Questions Series
www.vibrantpublishers.com
*****
C And C++ Interview Questions You'll Most Likely Be Asked
Copyright 2021, By Vibrant Publishers, USA. All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior permission of the publisher.
This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. The author has made every effort in the preparation of this book to ensure the accuracy of the information. However, information in this book is sold without warranty either expressed or implied. The Author or the Publisher will not be liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Vibrant Publishers books are available at special quantity discount for sales promotions, or for use in corporate training programs. For more information please write to bulkorders@vibrantpublishers.com
Please email feedback / corrections (technical, grammatical or spelling) to spellerrors@vibrantpublishers.com
To access the complete catalogue of Vibrant Publishers, visit www.vibrantpublishers.com
*****
Table of Contents
1. Memory Allocation
2. Structure and Union
3. Functions or Methods
4. Recursion
5. Pointer and its Handling
6. Templates
7. General Concepts
8. Control Flow Statements
9. Data Types, Variables and Operators
10. Macros, typedef, enum
11. Library Functions
12. Arrays
13. Files in C and File Handling
14. Exception Handling in C++
15. Memory Areas
16. Classes and its Properties
HR Questions
INDEX
*****
C And C++ Interview Questions
Review these typical interview questions and think about how you would answer them. Read the answers listed; you will find best possible answers along with strategies and suggestions.
*****
Memory Allocation
1: Explain the types of memories allocated in C & C++.
Answer:
C & C++ allocates memory as static and dynamic broadly. Static memory is where all the static variables are stored, irrespective of where they are declared within the program. Once the static variables are allocated memory, then the rest of the memory is used for allocating the dynamic variables. Everything other than the static variables is stored in heap and stack. All the variables local to a function are stored in a stack. All the other dynamically allocated variables are stored in a heap. Dynamic memory allocation is unpredictable and it may fail if the system does not have large chunks of free memory as requested. With C’s malloc() this is a major issue as for real-time and embedded systems, if malloc() fails it returns NULL which fails the program. In C++, dynamic memory allocation is done using new which is better equipped to allocate memory by reallocating smaller chunks of data as required. Moreover, new does not return a null and instead exits the program. This makes dynamic memory allocation in C++ superior and more reliable.
2: Explain Memory Fragmentation.
Answer:
Memory fragmentation occurs when C’s heap does not have contiguous blocks of memory as a single block to be allocated when it is being requested. Consider if your heap has 12K of memory and a 4K is allocated upon the first request with malloc(). Another 2K is requested and that is also allocated, now the heap has a contiguous memory of 6K to be allocated (table 1). If the first 4K memory is freed using the free() and now the heap actually has 10K of memory to be allocated. But it is not contiguous (table 2).
Total 12K of Memory in Heap (Table 1)
tmp_27b2d2d37ef4f35803a8243fa0886b44_gRalw2_html_m7ec040c3.jpgTotal 12K of Memory in Heap (Table 2)
tmp_27b2d2d37ef4f35803a8243fa0886b44_gRalw2_html_ad912a.jpgNow if heap gets a request for 8K of memory, the malloc() will fail even though the heap has free memory, but because it is not contiguous. This cannot be defragmented since during defragmentation, if the 2K lot is moved to the first lot address, the pointer to Req2 will become useless. In Java and other languages that have automatic garbage collection, the issue of fragmentation does not occur as these languages do not support direct access of address through pointers.
3: How is dynamic memory allocation in C different from that in C++?
Answer:
C uses malloc() and free() to allocate and de-allocate memory while C++ uses the new and delete operator to allocate and free memory dynamically. Malloc() is a void function that allocates memory dynamically according to the data type to be created. New is an operator that calls the constructor of the data type or objects to be created and returns the data type created. New never returns NULL. It will throw an exception or terminate the program when it fails. When we call new, the memory is allocated from the free storage whereas malloc() allocates memory from the heap. Malloc() may return NULL when it fails. You have to specify the size to be allocated in bytes when using malloc(). There’s an exclusive version of new for dynamically allocating arrays whereas with malloc() you have to calculate the array size and then allocate. The new and delete operators can be overridden whereas malloc() and free() cannot be overridden.
4: Can malloc() fail? How do you handle it?
Answer:
Though it is unlikely that malloc() will fail for a normal program, there is no 100% assurance that malloc() will not fail. For bigger program that request bigger chunks of memory allocation, there is a possibility that malloc() may fail if the system does not have sufficient memory to allocate, is too heavily loaded to allocate memory during the malloc() call or the program is requesting way too large memory than anticipated. Another possible issue is that since malloc() accepts only unsigned int, if you are using pointer arithmetic to find the amount of memory required and get a huge negative figure, malloc() considers it as a positive figure which will be very huge amount of memory to allocate and may fail. Checking for a NULL pointer and exiting the program gracefully one way you can handle the situation. If you do not exit the program and the program aborts, you will have to be prepared for some unwanted surprises. So the best way to handle malloc() is to check for Null pointer and exit the program if malloc() has failed.
5: What is Dynamic Memory Allocation in C and how it is achieved in C Programming?
Answer:
In a C program there are two ways memory can be allocated for the data variables. One of these is to allocate the memory at the runtime, i.e. while the program is in execution mode or when loader and linker finish their task and the program along with all required library functions are residing in the memory. At this stage, memory can be allocated for the program from the ‘heap’. This method of allocating memory for data variables during program run time is called Dynamic Memory Allocation.
Dynamic Memory Allocation can be achieved by two C library functions ‘malloc’ and ‘calloc’ which use Pointer concepts to allocate memory at the run time. The amount of memory required is passed as argument to these functions and the variable for amount of memory to be allocated can be entered by the user while the program is in the middle of its execution; thus achieving runtime memory management in C.
6: What is the difference if dynamic memory is allocated using library functions malloc and calloc?
Answer:
Both the functions allocate memory dynamically, but there is a difference in the way they do it.
Malloc allocates memory in contiguous manner by using the input of how much memory needs to be allocated, then allocates a large chunk of contiguous memory defined in the parameter. Calloc allocates memory that may not be contiguous. It uses two parameters for the number of blocks to be allocated and size of each memory block.
Malloc never initializes its allocated memory , while calloc initializes all the allocated memory blocks to zero.
7: How is run time memory management achieved in C++?
Answer:
There are two operators which perform the task of allocating or
releasing memory in C++ run time; new
and delete
.
While allocating dynamic memory using new, it takes one integer as an argument and returns a pointer of type for which new is allocating memory.
* new datatype[integer]; Example – char *Ptr_char = new char[10]
In the example, the operation allocates memory for 10 characters or 10 bytes dynamically.
The delete operator takes only the pointer variable that was used while allocating the memory that is going to be released by delete.
delete
8: How is the realloc() function used in C?
Answer:
This function is used to reallocate memory for a predefined memory block to either increase or decrease the already allocated memory. It takes one pointer argument and one integer argument mentioning the new amount of memory block to be allocated. The pointer is the one carrying the starting address of a memory block that was previously allocated. Through realloc(),memory newly assigned may be new memory allocated that is entirely from a different memory place.
*****
Structure and Union
9: Explain with example how a structure can be passed to a function in C.
Answer:
A structure can be passed by value or by reference to a function in C just like any other data type. The following example will explain how to pass a structure by value to a function.
#include
struct custRecord {
char *cust_Name;
int cust_Id;
char account_Type;
float acct_Balance;
};
int main()
{
struct custRecord addInt(struct custRecord);
struct custRecord newCustomer = {Anita
, 29017, 'S', 25892.23};
printf(\nCustomer Details\n
);
printf(%s, %d, %c, %f
, newCustomer.cust_Name, newCustomer.cust_Id, newCustomer.account_Type, newCustomer.acct_Balance);
newCustomer = addInt(newCustomer);
printf(\nNew Customer Details\n
);
printf("%s, %d, %c,