0% found this document useful (0 votes)
2 views30 pages

Fundamentals of Programming

This document provides comprehensive notes on programming fundamentals, particularly focusing on C++ concepts, problem-solving life cycles, algorithms, and object-oriented programming. It covers various topics including the phases of problem-solving, algorithm design, C++ fundamentals like variable declaration, data types, operators, arrays, control structures, and functions. Additionally, it includes related questions and answers to reinforce understanding of the material.

Uploaded by

yaredberhanu246
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)
2 views30 pages

Fundamentals of Programming

This document provides comprehensive notes on programming fundamentals, particularly focusing on C++ concepts, problem-solving life cycles, algorithms, and object-oriented programming. It covers various topics including the phases of problem-solving, algorithm design, C++ fundamentals like variable declaration, data types, operators, arrays, control structures, and functions. Additionally, it includes related questions and answers to reinforce understanding of the material.

Uploaded by

yaredberhanu246
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/ 30

Fundamentals of Programming

Fundamentals of Programming: Comprehensive


Notes
This document provides detailed notes on key programming concepts, focusing on C++
fundamentals, problem-solving, algorithms, and object-oriented programming (OOP). It includes
explanations and solutions to related questions for better understanding.

1. Problem-Solving Life Cycle


The problem-solving life cycle in programming consists of distinct phases to ensure efficient
software development.

1.1 Analysis (Problem Specification)


This initial phase focuses on understanding the problem by identifying:

Input Data: What information is required?


Procedure: Steps to transform input into output.
Expected Output: The desired result.

Related Question:

Q1. In which step of the problem-solving life cycle are the input data, procedure, and expected
output identified?

A. Analysis (problem specification)


B. Algorithm design
C. Implementation or coding
D. Maintenance and documentation

Answer: A. Analysis (problem specification)

1.2 Algorithm Design


Develop a step-by-step procedure (algorithm) to solve the problem based on the analysis.
1.3 Implementation or Coding
Translate the algorithm into code using a programming language like C++.

1.4 Testing
Verify the program's correctness and ensure it meets all requirements by identifying and fixing
errors (bugs).

Related Question:

Q26. Which phase of the problem-solving life cycle verifies correctness and ensures
requirements are met?

A. Testing
B. Debugging
C. Analysis
D. Maintenance

Answer: A. Testing

1.5 Maintenance and Documentation


Update, improve, and fix issues in the software post-deployment. Proper documentation is
essential throughout the cycle.

2. Algorithms
An algorithm is a finite set of well-defined instructions to accomplish a task. A good algorithm
has specific properties, such as finiteness.

2.1 Finiteness Property


An algorithm must terminate after a finite number of steps for all input combinations.

Related Question:

Q2. What is the finiteness property of an algorithm?

A. The number of steps in the algorithm should be finite.


B. The algorithm should terminate after a finite number of times.
C. For all possible combinations of input data, the algorithm terminates after a finite number
of steps.
D. A and C

Answer: D. A and C

3. Programming Approaches
Different approaches guide software design and development.

3.1 Top-Down Approach


Start with the overall system and break it into smaller, manageable modules until they are
simple enough to code.

Related Questions:

Q3. Which approach is used by C++?

A. Left-right
B. Bottom-up
C. Right-left
D. Top-down

Answer: D. Top-down (C++ supports top-down design through functions and object-oriented
design).

Q49. Which approach is used by C++?

A. Left-right
B. Right-left
C. Bottom-up
D. Top-down

Answer: D. Top-down (Repeated question, same answer).

4. C++ Fundamentals
C++ is a powerful, general-purpose programming language with robust features.
4.1 Variable Declaration and Naming
Variables must be declared with a data type, and naming follows specific rules.

Related Questions:

Q31. Which is not a legal variable name in C++?

A. _something
B. aVariable
C. float2string
D. 2manyLetters
E. X

Answer: D. 2manyLetters (Variable names cannot start with a number).

Q100. Which is a correct identifier in C++?

A. VAR_1234
B. varname
C. 7VARNAME
D. 7varname

Answer: A. VAR_1234 (Identifiers cannot start with a number or contain special characters like
' ).

4.2 Data Types


C++ offers various data types to store different kinds of values.

Related Questions:

Q32. Which numeric data type has the largest range?

A. int
B. char
C. float
D. double

Answer: D. double (It typically has the largest range for floating-point numbers).

Q43. Which is different from the others?

A. int a;
B. float b;
C. double d;
D. int c = 40;

Answer: D. int c = 40; (This includes initialization, unlike the others which are declarations
only).

4.3 Operators
Operators perform operations on variables and values.

Related Questions:

Q15. Which statement is correct about the increment operator?

A. Increment operator ( ++ ) usually adds 2 to its operand.


B. Decrement operator ( ++ ) subtracts 1 from its operand.
C. Decrement operator ( ++ ) subtracts 3 from its operand.
D. Increment operator ( ++ ) usually adds 1 to its operand.

Answer: D. Increment operator ( ++ ) usually adds 1 to its operand.

Q16. Which statement about pre-increment is true?

A. Pre-increment is usually faster than post-increment.


B. Post-increment is faster than pre-increment.
C. Pre-increment is slower than post-increment.
D. Pre-decrement is slower than post-increment.

Answer: A. Pre-increment is usually faster than post-increment (It avoids creating a temporary
copy).

Q17. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << --x + 1 << ",";
cout << x++;
return 0;
}
Explanation:

x = 10
--x : x becomes 9, expression evaluates to 9.
--x + 1 : 9 + 1 = 10 , prints 10 .
x++ : Current x (9) is printed, then x becomes 10.

Answer: A. 10,9

Q18. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
int k = 2, g = 20;
k *= g++;
cout << k << " , " << g;
return 0;
}

Explanation:

k = 2 , g = 20
k *= g++ : Equivalent to k = k * (g++) .
g++ uses g ’s current value (20), then increments g to 21.
k = 2 * 20 = 40 .
Print k (40) and g (21).

Answer: B. 40 , 21

Q19. What is the value of var ?

int var;
var = 3 + 2 * 3 / 2 - (4 - 2);

Explanation (PEMDAS/BODMAS):

1. Parentheses: (4 - 2) = 2
2. Multiplication/Division (left to right):
2 * 3 = 6
6 / 2 = 3
3. Addition/Subtraction: 3 + 3 - 2 = 4
Answer: C. 4

Q20. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
int a, b, c;
a = 2;
b = 7;
c = (a > b) ? a : b;
cout << c;
return 0;
}

Explanation:

a = 2, b = 7
c = (a > b) ? a : b : (2 > 7) is false, so c = b = 7 .
Prints 7 .

Answer: D. 7

Q21. What is the value stored in c ?

int a = 4, b = 9, c;
c = a++ + ++b;

Explanation:

a = 4, b = 9
a++ : Uses 4 , then a becomes 5.
++b : b becomes 10, uses 10.
c = 4 + 10 = 14 .

Answer: C. 14

4.4 Arrays
Arrays store similar data items in contiguous memory locations.

Related Questions:

Q4. Which correctly declares an array in C++?


A. array{10};
B. int array;
C. array array[10];
D. int array[10];

Answer: D. int array[10];

Q7. Which describes characteristics of an array?

A. A set of similar data items


B. A set of distinct data items
C. Can hold different data types
D. None of the above

Answer: A. A set of similar data items (Arrays store elements of the same type).

Q8. What can be considered the object of an array?

A. Index of an array
B. Elements of the array
C. Functions of the array
D. All of the above

Answer: B. Elements of the array

Q9. An out-of-range array index in C++ causes what type of error?

A. Compile time
B. Run time
C. Not an error
D. None

Answer: B. Run time (Accessing out-of-bounds indices leads to undefined behavior, typically a
runtime error).

Q10. Which correctly declares an array in C++?

A. array{10};
B. array array[10];
C. int array;
D. int array[10];

Answer: D. int array[10]; (Repeated question, same answer).


Q11. Which is not true about arrays?

A. Heterogeneous datatype
B. Homogeneous datatype
C. Store continuous data
D. Access elements through index

Answer: A. Heterogeneous datatype (Arrays store homogeneous data).

Q12. Which is the correct syntax for declaring an array of pointers to integers with size 10 in
C++?

A. int arr = new int[10];


B. int *arr = new int[10];
C. int arr = new int[10];
D. int *arr = new int[10];

Explanation:

int *arr = new int[10]; declares an array of 10 integers.


int **arr = new int*[10]; declares an array of 10 pointers to integers.

Answer: D. int *arr = new int[10]; (Note: The question asks for an array of pointers, but
the options suggest an array of integers. Assuming a typo, the correct answer is for an array of
integers).

Q13. What is the output of the following program?

#include <iostream>
using namespace std;
int main() {
int i = {1, 2, 3};
cout << i << endl;
return 0;
}

Explanation: Initializing an int with {1, 2, 3} is invalid; it’s meant for arrays or aggregate
types. This causes a compile-time error.

Answer: D. Error

Q14. What does the following Java code print?


int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b);

Explanation: In Java, == compares array references, not contents. Since a and b are
distinct objects, the output is false .

Answer: B. It prints false .

4.5 Control Structures (Loops and Decisions)


Control structures manage the flow of execution.

4.5.1 Conditional Statements


Related Questions:

Q38. Which instruction implements decision-making for doing one thing or another?

A. while
B. for
C. if..else
D. sequence
E. cin

Answer: C. if..else

Q45. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
float mark = 50.0L;
if (mark > 85)
cout << "Your grade is A ";
cout << "Excellent!";
return 0;
}

Explanation:

mark = 50.0
if (mark > 85) is false, so cout << "Your grade is A "; is skipped.
cout << "Excellent!"; is outside the if block and always executes.

Answer: C. Excellent!

Q46. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
if (6 > 8) {
cout << "**" << endl;
cout << "****" << endl;
}
else if (9 == 4)
cout << "***" << endl;
else
cout << "*" << endl;
return 0;
}

Explanation:

6 > 8 is false.
9 == 4 is false.
The else block executes, printing * .

Answer: C. *

4.5.2 Loops (Repetition)


Related Questions:

Q19. Which is the correct syntax for a for loop?

A. for(initialization; condition; increment/decrement operator){}


B. for(initialization, condition; increment/decrement operator){}
C. for(initialization; increment/decrement operator; condition){}
D. None of the above

Answer: A. for(initialization; condition; increment/decrement operator){}

Q29. Which loop is typically used for a menu-driven program?

A. Do While Loop
B. For Loop
C. While Loop
D. Do Loop

Answer: A. Do While Loop (Ensures the menu displays at least once).

Q30. What is the correct order of execution in a for loop?

for (initialization; condition; increment/decrement) {


// body of the loop
}

i. Condition is checked
ii. Initialization is executed
iii. Increment/Decrement
iv. Statement is executed

Answer: A. ii, i, iv, iii (Initialization, condition check, body execution, increment/decrement).

Q30. How many times is "in the loop" printed?

#include <iostream>
using namespace std;
int main() {
int a = 4, b = 12;
do {
cout << "in the loop" << endl;
a += 2;
b -= 2;
} while (a < b);
return 0;
}

Explanation:

Iteration a b a<b Output


1 4 12 True in the loop
2 6 10 True in the loop
3 8 8 False (terminates)
Answer: B. 2

Q31. How many times does sum get incremented in the following code?

#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum++;
}
}
cout << sum;
return 0;
}

Explanation:

Outer loop ( i ): 3 iterations.


Inner loop ( j ): 3 iterations per outer loop.
Total: 3 * 3 = 9 increments.

Answer: C. 9

4.5.3 Loop Control Statements


Related Question:

Q27. A statement that skips the remaining statements in a loop and proceeds to the next
iteration is:

A. Break statement
B. Continue statement
C. Case statement
D. None of them

Answer: B. Continue statement

4.6 Functions
Functions are reusable code blocks for specific tasks.

Related Questions:
Q42. Which is not part of every C++ function?

A. Function header
B. Function body
C. Function selector
D. Function parameters
E. All except C

Answer: C. Function selector (No such term exists in C++).

Q58. What is the following statement?

int addition(int, int);

A. Function invocation
B. Function prototyping
C. Not a valid C++ statement
D. Variable declaration

Answer: B. Function prototyping

Q59. Which statement is false?

A. Function has header and body


B. Parameters can be passed to a function
C. Void type has a return value of 1
D. Void type has return value of empty

Answer: C. Void type has a return value of 1 ( void returns no value).

4.6.1 Inline Functions


Related Questions:

Q17. Why are inline functions useful?

A. Functions are large with nested loops


B. Small functions to avoid function call overhead
C. Functions with static variables
D. All of the above

Answer: B. Small functions to avoid function call overhead


Q87. What is the effect of small inline functions on cache misses?

A. Increase cache misses


B. Decrease cache misses
C. No cache misses
D. None of these

Answer: B. Decrease cache misses (Inlining improves locality of reference).

4.6.2 Pass by Value vs. Pass by Reference


Related Questions:

Q55. Which is more effective for calling C++ functions?

A. Call by object
B. Call by pointer
C. Call by value
D. Call by reference

Answer: D. Call by reference (Avoids copying large objects, safer than pointers).

Q72. What is pass by value?

A. Pass the exact value of the variable to the function


B. Pass the address of the variable to the function
C. Pass the size of the variable to the function
D. All the above

Answer: A. Pass the exact value of the variable to the function

Q73. What happens with pass by reference?

A. Values are passed to manipulate


B. Memory location is passed to use the same memory area
C. Function declaration contains &
D. All of the mentioned

Answer: D. All of the mentioned

Q74. Given the function:


void calculateArea(float& a, float r) {
a = r * r * 3.14;
}

With float area = 0; float radius = 5; in main() , how is it called?

A. calculateArea(area, radius);
B. area = calculateArea(radius);
C. calculateArea(&area, radius);
D. calculateArea(area);

Answer: A. calculateArea(area, radius); (First parameter is a reference, second is by


value).

4.7 Pointers and References


Pointers store memory addresses; references are aliases.

Related Questions:

Q12. How are references different from pointers?

A. References cannot be modified once initialized


B. No extra operator needed for dereferencing
C. References cannot be NULL
D. All of the above

Answer: D. All of the above

Q71. What is a pointer?

A. Stores a series of data


B. Stores exact values of variables
C. Stores the address of another variable
D. Stores the size of a variable

Answer: C. Stores the address of another variable

Q72. What happens in the following code?

int *ptr = NULL;


delete ptr;
Answer: B. Compiles and executes successfully (Deleting a NULL pointer is safe).

Q73. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}

Explanation: Taking the address of a register variable is not allowed, causing a compile-
time error.

Answer: B. Compiler error may be possible

Q74. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10, c = 15;
int arr[3] = {&a, &b, &c}; // Incorrect: int* arr[3] needed
cout << *arr[*arr[1] - 8];
return 0;
}

Explanation: int arr[3] = {&a, &b, &c}; causes a compile-time error due to type
mismatch. If corrected to int* arr[3] , the expression *arr[*arr[1] - 8] evaluates to 15 .

Answer: D. Compile-time error

Q75. Which is not a correct way to pass a pointer to a function?

A. Non-constant pointer to non-constant data


B. Non-constant pointer to constant data
C. Constant pointer to non-constant data
D. All of the above

Answer: D. All are correct ways


5. Object-Oriented Programming (OOP) in C++
OOP is based on objects and classes.

5.1 Core OOP Concepts


5.1.1 Reusability

Related Questions:

Q5. Which concept allows code reuse in C++?

A. Inheritance
B. Polymorphism
C. Abstraction
D. Encapsulation

Answer: A. Inheritance

Q10. Which statement supports reusable code as a desirable feature?

A. Reduces maintenance cost


B. Reduces testing time
C. Reduces both maintenance and testing time
D. Reduces compile time

Answer: C. Reduces both maintenance and testing time

Q11. Which concept allows code reuse in C++?

A. Inheritance
B. Polymorphism
C. Abstraction
D. Encapsulation

Answer: A. Inheritance (Repeated question).

5.1.2 Classes and Objects

Related Questions:

Q9. Which statement is correct about classes?

A. An object is an instance of its class


B. A class is an instance of its object
C. An object is the instance of the data记者

System: type of that class

D. Both A and C

Answer: D. Both A and C (An object is an instance of a class, defining its data type).

Q75. When an ADT is implemented as a C++ class, which is typically true?

A. Member functions are private, member variables are public


B. Member functions are public, member variables are private
C. Both are private
D. Both are public

Answer: B. Member functions are public, member variables are private (Encapsulation
principle).

5.1.3 Access Specifiers


Related Questions:

Q6. To which are access specifiers applicable?

A. Member data
B. Functions
C. Both member data & functions
D. Protected members

Answer: C. Both member data & functions

Q68. Which access modifier is used in a constructor by default?

A. Protected
B. Public
C. Private
D. Either A or C

Answer: B. Public (Constructors are typically public for external instantiation).

5.1.4 this Pointer


Related Questions:
Q24. Which is correct about the this pointer in C++?

A. Passed as a hidden argument in all static variables


B. Passed as a hidden argument in all functions
C. Passed as a hidden argument in all non-static functions
D. Passed as a hidden argument in all static functions

Answer: C. Passed as a hidden argument in all non-static functions

Q52. (Repeated question, same answer: C)

5.1.5 Inheritance
Related Questions:

Q88. If a derived class object is created, which constructor is called first?

A. Derived class constructor


B. Base class constructor
C. Depends on how the object is called
D. Both A and B

Answer: B. Base class constructor

Q94. Identify the correct statements:

i. Derived classes do not inherit or overload constructors or destructors


ii. Destructors can be declared with virtual
iii. Constructors can be declared with virtual
A. (i) and (iii) only
B. (i) and (ii) only
C. (ii) and (iii) only
D. (i), (ii), and (iii)

Answer: B. (i) and (ii) only (Constructors cannot be virtual).

5.1.6 Polymorphism
Related Question:

Q86. Which feature allows objects with different internal structures to share the same external
interface?
A. Objects
B. Classes
C. Polymorphism
D. Message passing

Answer: C. Polymorphism

5.1.7 Encapsulation and Abstraction


Related Question:

Q60. What is an abstract class in C++?

A. Any class
B. Class from which any class is derived
C. Class with at least one virtual function
D. Class with at least one pure virtual function

Answer: D. Class with at least one pure virtual function

5.1.8 Structures vs. Classes

Related Questions:

Q62. How do structures and classes differ in C++?

A. Structures hide members by default, classes do not


B. Structures have public members by default, classes have private
C. Structures cannot have private members, classes can
D. Structures have private members by default, classes have public

Answer: B. Structures have public members by default, classes have private

Q66. Which is not correct about structures?

A. Programmer-defined data types


B. Can contain different data types
C. Members accessed through structure variable
D. Default passing is by reference

Answer: D. Default passing is by reference (Default is by value).

5.2 Constructors
Related Question:

Q89. For the following program:

class Example {
public:
int a, b, c;
Example() { a = b = c = 1; } // Constructor 1
Example(int a) { a = a; b = c = 1; } // Constructor 2
Example(int a, int b) { a = a; b = b; c = 1; } // Constructor 3
Example(int a, int b, int c) { a = a; b = b; c = c; } // Constructor 4
};

Which constructor is called by Example obj = new Example(1, 2, 3); ?

Explanation: Matches Constructor 4, but a = a does not assign to the member variable (use
this->a = a ).

Answer: B. Constructor 4

6. Input/Output (I/O)
C++ uses streams for I/O operations.

Related Questions:

Q67. Which pair is incorrectly matched?

A. (cin, read from keyboard)


B. (cout, write to screen)
C. (ofstream, read from a file)
D. (fstream, write to a file)

Answer: C. (ofstream, read from a file) ( ofstream is for writing).

Q69. Which statement is true?

A. ifstream can create a file


B. ofstream cannot create a file
C. ofstream is used to write a file
D. A and B
Answer: C. ofstream is used to write a file

7. Exception Handling
Exception handling manages errors gracefully.

Related Questions:

Q14. What happens when the try block is moved far away from the catch block?

A. Reduces code in cache


B. Increases code in cache
C. Doesn’t alter anything
D. Increases the amount of code

Explanation: Syntactically, catch must follow try , or it’s a compile-time error. If logically
separated (e.g., in different functions), exception handling may increase code size due to stack
unwinding.

Answer: D. Increases the amount of code

Q59. Code causing abnormal termination should be written in which block?

A. catch
B. throw
C. try
D. finally

Answer: C. try

8. Compilation and Execution Process


Related Questions:

Q28. What is the correct order for program execution?

A. Object code, Source code, Executable code


B. Source code, Object code, Executable code
C. Running, Debugging, Compiling
D. Running, Compiling, Debugging

Answer: B. Source code, Object code, Executable code

Q80. Place the following in correct order:

I. Linking
II. Compiling
III. Execution
IV. Processing #include directives

Answer: C. IV, II, I, III

8.1 Preprocessor Directives


Related Questions:

Q23. Which statement is incorrect about preprocessor directives?

A. Processed by the preprocessor


B. Do not produce code
C. Must be on their own line
D. End with a semicolon

Answer: D. End with a semicolon

Q96. Which statement is true about preprocessor directives?

A. Processed by the preprocessor


B. Do not produce code
C. Must be on their own line
D. End with a semicolon

Answer: A. Processed by the preprocessor

9. Operating System (OS) Tasks


Related Question:

Q41. Tasks performed by the OS include:

A. Management of secondary storage devices


B. Memory management
C. Allocation of CPU time
D. All

Answer: D. All

10. Command Line Arguments


Related Questions:

Q13. Which refers to command line arguments?

A. Arguments passed to main()


B. Arguments passed to structure-function
C. Arguments passed to class functions
D. Arguments passed to any functions

Answer: A. Arguments passed to main()

Q21. How to handle command line arguments with spaces?

A. Use single quotes


B. Either single or double quotes
C. Use double quotes
D. No way to handle spaces

Answer: C. Use double quotes

11. Miscellaneous C++ Concepts


11.1 Dynamic Loading / Dynamic Binding
Related Question:

Q11. Which concept refers to adding components at runtime?

A. Dynamic Loading
B. Dynamic binding
C. Data hiding
D. Both A & B

Answer: A. Dynamic Loading

11.2 Modularity
Related Question:

Q20. The term modularity refers to:

A. Dividing the program into independent parts


B. Overriding parts of the program
C. Wrapping things into a single unit
D. None of the above

Answer: A. Dividing the program into independent parts

11.3 Associativity of Operators


Related Question:

Q18. Which has left-to-right associativity?

A. Addressof
B. Unary operator
C. Logical not
D. Array element access

Answer: D. Array element access

11.4 goto Statement


Related Question:

Q44. How many times will the print statement execute?

#include <iostream>
using namespace std;
int main() {
int i = 0;
label:
cout << "Interviewbit";
i++;
if (i < 3) {
goto label;
}
return 0;
}

Explanation: Executes 3 times ( i = 0, 1, 2 ).

Answer: C. 3 times

11.5 Scope of Variables


Related Question:

Q37. A variable declared inside a function is called?

A. Local variable
B. Global variable
C. Normal variable
D. Variable scope

Answer: A. Local variable

11.6 const Keyword


Related Question:

Q77. What is wrong with the following code?

#include <iostream>
using namespace std;
int main() {
int x = 4, y = 3; // line 1
const int i = 10; // line 2
x = x + i; // line 3
i = y * 2; // line 4
return 0;
}

Explanation: Assigning to a const variable ( i = y * 2 ) causes a compile-time error.

Answer: D. Error in line 4

11.7 Declaration vs. Definition


Related Question:

Q25. What is the difference between declaration and definition?

A. Both can occur multiple times, declaration first


B. Definition occurs once, declaration many times
C. Declaration occurs once, definition many times
D. Both can occur multiple times, definition first

Answer: B. Definition occurs once, declaration many times

11.8 Standard Library Usage


Related Questions:

Q90. What is the output of the following code?

#include <iostream>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs"};
unsigned long int found = str.find_first_of("aeiou");
while (found != string::npos) {
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
cout << str << "\n";
return 0;
}

Explanation: Replaces vowels with * , resulting in St*v* j*bs .

Answer: C. St*v* j*bs

Q92. What is the output of this program?

#include <iostream>
using namespace std;
int array1[] = {1200, 200, 2300, 1230, 1543};
int array2[] = {12, 14, 16, 18, 20};
int temp, result = 0;
int main() {
for (temp = 0; temp < 5; temp++) {
result += array1[temp];
}
for (temp = 0; temp < 4; temp++) {
result += array2[temp];
}
cout << result;
return 0;
}

Explanation: result = 1200 + 200 + 2300 + 1230 + 1543 + 12 + 14 + 16 + 18 = 6533 .

Answer: B. 6533

Q93. What happens in these programs?

// Program 1
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1;
arr1.fill(5);
cout << get<5>(arr1);
return 0;
}

// Program 2
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> arr1;
arr1.fill(5);
cout << arr1.at(5);
return 0;
}

Explanation:

Program 1: get<5> causes a compile-time error (out-of-bounds).


Program 2: arr1.at(5) throws a runtime std::out_of_range exception.

Answer: A. Program 1: compile-time error; Program 2: run-time error

11.9 Bitwise Operators


Related Question:

Q22. What is the value of p ?

#include <iostream>
using namespace std;
int main() {
int p;
bool a = true;
bool b = false;
int x = 10;
int y = 5;
p = ((x | y) + (a + b));
cout << p;
return 0;
}

Explanation: (x | y) = (1010 | 0101) = 1111 = 15 , a + b = 1 + 0 = 1 , p = 15 + 1 =


16 .

Answer: D. 16

11.10 Character Output


Related Question:

Q56. What is the output of the following code?

#include <iostream>
using namespace std;
int main() {
char c = 74;
cout << c;
return 0;
}

Explanation: char c = 74 assigns ASCII value 74, which is 'J' .

Answer: B. J

You might also like