Karnataka II PUC Computer Science (KSEAB) study materials
Karnataka II PUC Computer Science (KSEAB) study resources

Karnataka II PUC Computer Science (KSEAB) practice questions

One question at a time. Build your Karnataka II PUC Computer Science (KSEAB) understanding.

First 50 of 199 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Overview of Computers, Input/Output and Memory Devices, Data Representation

Which part of the CPU performs arithmetic and logical operations on data?

  1. ALU Correct answer.
  2. Memory unit
  3. Input unit
  4. Control unit

Explanation

The CPU is organised into the control unit, the arithmetic logic unit and registers. The ALU carries out arithmetic operations such as addition and subtraction as well as logical operations such as comparison, AND and OR, whereas the control unit only fetches and directs instructions.

Question 2Overview of Computers, Input/Output and Memory Devices, Data Representation

Which of the following is an output device?

  1. Light pen
  2. Plotter Correct answer.
  3. Joystick
  4. Scanner

Explanation

Output devices such as monitors, printers and plotters deliver processed results from the computer to the user. A light pen, a joystick and a scanner all send data into the computer, so they are classified as input devices.

Question 3Overview of Computers, Input/Output and Memory Devices, Data Representation

In the binary convention used in computer science, one kilobyte (KB) is equal to

  1. 512 bytes
  2. 2048 bytes
  3. 1024 bytes Correct answer.
  4. 1000 bytes

Explanation

Computer memory is organised in powers of two, so the binary meaning of the kilo prefix is 2^10, which equals 1024. Therefore 1 KB = 1024 bytes and 1 MB = 1024 KB; the value 1000 belongs to the decimal SI system, not to binary memory addressing.

Question 4Overview of Computers, Input/Output and Memory Devices, Data Representation

Which list arranges memory units in increasing order of access time, starting with the fastest?

  1. Cache, registers, main memory, secondary storage
  2. Main memory, cache, registers, secondary storage
  3. Registers, main memory, cache, secondary storage
  4. Registers, cache, main memory, secondary storage Correct answer.

Explanation

Registers are located inside the CPU and are accessed within a single clock cycle, cache is a small fast memory placed close to the CPU, main memory (RAM) is larger but slower, and secondary storage such as disks is the slowest of all. Hence the fastest-first sequence is registers, cache, main memory, secondary storage.

Question 5Overview of Computers, Input/Output and Memory Devices, Data Representation

Which of the following is an optical storage medium?

  1. Hard disk
  2. Floppy disk
  3. Magnetic tape
  4. DVD Correct answer.

Explanation

A DVD stores data as microscopic pits and lands on a reflective surface, and the pattern is read by a laser beam, which makes it an optical medium. Hard disks, floppy disks and magnetic tapes record data as magnetised spots on a magnetic coating, so they belong to the magnetic category.

Question 6Overview of Computers, Input/Output and Memory Devices, Data Representation

The binary equivalent of the decimal number 45 is

  1. 101011
  2. 110101
  3. 110011
  4. 101101 Correct answer.

Explanation

Since 45 = 32 + 8 + 4 + 1, the bits in positions 5, 3, 2 and 0 are set and all other bits are 0, which gives 101101. Repeated division by 2 (45 gives remainder 1, 22 gives 0, 11 gives 1, 5 gives 1, 2 gives 0, 1 gives 1) and reading the remainders upwards gives the same result.

Question 7Overview of Computers, Input/Output and Memory Devices, Data Representation

The sum of the binary numbers 1011 and 0110, expressed in binary, is

  1. 10001 Correct answer.
  2. 11101
  3. 1001
  4. 1101

Explanation

Adding column by column from the right: 1 + 0 gives 1; 1 + 1 gives 0 with a carry of 1; 0 + 1 + 1 gives 0 with a carry of 1; and 1 + 0 + 1 gives 0 with a carry of 1, so the final carry makes the result 10001. In decimal this checks as 11 + 6 = 17, and 10001 is indeed 17.

Question 8Overview of Computers, Input/Output and Memory Devices, Data Representation

Which character encoding standard is designed to represent characters of almost all the languages of the world?

  1. ISCII
  2. EBCDIC
  3. ASCII
  4. Unicode Correct answer.

Explanation

Unicode assigns a unique code point to characters of virtually every writing system, and its UTF-8 and UTF-16 forms are used widely on the web and in modern operating systems. ASCII covers only 128 English letters, digits and control codes, EBCDIC is IBM's 8-bit set, and ISCII is restricted to Indian scripts.

Question 9Overview of Computers, Input/Output and Memory Devices, Data Representation

The range of decimal integers that can be stored in an 8-bit register using two's complement form is

  1. 0 to 255
  2. -127 to +127
  3. -128 to +128
  4. -128 to +127 Correct answer.

Explanation

In n-bit two's complement representation the values run from -2^(n-1) to +2^(n-1) - 1, so for n = 8 the range is -128 to +127. There is one more negative value than positive values because the all-zero pattern represents 0.

Question 10Overview of Computers, Input/Output and Memory Devices, Data Representation

The hexadecimal equivalent of the binary number 1011101011 is

  1. 2EB Correct answer.
  2. 3EB
  3. 2E3
  4. 2DB

Explanation

Group the bits into fours starting from the right, padding the leftmost group with leading zeros: 0010 1110 1011. Each group converts to one hexadecimal digit, giving 0010 = 2, 1110 = E (14) and 1011 = B (11), so the number is 2EB, which equals 747 in decimal.

Question 11Overview of Computers, Input/Output and Memory Devices, Data Representation

Which unit within the CPU directs the flow of instructions and data between the other units?

  1. Main memory unit
  2. Input/output unit
  3. Control unit Correct answer.
  4. Arithmetic logic unit

Explanation

The control unit fetches and decodes instructions and generates control signals that coordinate the other CPU units. The arithmetic logic unit performs calculations, while memory and I/O units store data or handle transfers; they do not direct the internal operation sequence.

Question 12Overview of Computers, Input/Output and Memory Devices, Data Representation

A device that senses pencil marks in predetermined positions on a printed answer sheet is called a/an

  1. Magnetic ink character reader
  2. Bar code reader
  3. Optical character reader
  4. Optical mark reader Correct answer.

Explanation

The optical mark reader detects the presence of pencil or pen marks in fixed positions, which is how OMR answer sheets are scored. OCR reads printed characters, MICR reads magnetic ink, and barcode readers decode bar patterns.

Question 13Overview of Computers, Input/Output and Memory Devices, Data Representation

The number of bits present in one nibble is

  1. 4 Correct answer.
  2. 16
  3. 2
  4. 8

Explanation

A nibble is defined as a group of 4 bits, which is half of a byte. It can represent 16 different values, from 0000 to 1111 in binary.

Question 14Overview of Computers, Input/Output and Memory Devices, Data Representation

A memory chip is provided with 12 address lines. How many bytes can it address, assuming one byte per address?

  1. 1024 bytes
  2. 4096 bytes Correct answer.
  3. 8192 bytes
  4. 2048 bytes

Explanation

Each address line doubles the number of addressable locations, so 12 lines give 2^12 = 4096 addresses. With one byte stored at each address, the capacity is 4096 bytes.

Question 15Overview of Computers, Input/Output and Memory Devices, Data Representation

Which type of ROM can be erased by exposing it to ultraviolet light and then reprogrammed?

  1. EPROM Correct answer.
  2. PROM
  3. EEPROM
  4. Mask ROM

Explanation

EPROM stands for erasable programmable read-only memory; its contents are erased by exposing a quartz window to ultraviolet light. PROM is one-time programmable, EEPROM is erased electrically, and mask ROM is programmed during manufacture.

Question 16Overview of Computers, Input/Output and Memory Devices, Data Representation

The binary equivalent of the decimal fraction 0.625 is

  1. 0.110
  2. 0.011
  3. 0.100
  4. 0.101 Correct answer.

Explanation

Multiply the fractional part repeatedly by 2 and record the integer parts: 0.625 × 2 = 1.25, 0.25 × 2 = 0.5, and 0.5 × 2 = 1.0. Reading the integer parts in order gives 0.101.

Question 17Overview of Computers, Input/Output and Memory Devices, Data Representation

Which memory is volatile and stores each bit using a flip-flop circuit?

  1. Flash memory
  2. Read-only memory
  3. Static RAM Correct answer.
  4. Dynamic RAM

Explanation

Static RAM stores each bit in a flip-flop and remains volatile, so it loses its contents when power is removed. Dynamic RAM uses capacitors, ROM is non-volatile, and flash memory retains data without power.

Question 18Overview of Computers, Input/Output and Memory Devices, Data Representation

Using 8-bit signed magnitude representation, the binary pattern for -5 is

  1. 10000101 Correct answer.
  2. 11111011
  3. 11111010
  4. 00000101

Explanation

In signed magnitude form, the leftmost bit is the sign bit and the remaining seven bits hold the magnitude. For -5 the sign bit is 1 and 5 is 0000101, giving 10000101.

Question 19Overview of Computers, Input/Output and Memory Devices, Data Representation

The octal equivalent of the hexadecimal number 2F is

  1. 56
  2. 75
  3. 57 Correct answer.
  4. 47

Explanation

Convert 2F from hexadecimal to decimal: 2 × 16 + 15 = 47. Then convert 47 to octal by dividing by 8: 47 = 5 × 8 + 7, so the octal form is 57.

Question 20Overview of Computers, Input/Output and Memory Devices, Data Representation

The 8-bit two's complement representation of -128 is

  1. 01111111
  2. 10000001
  3. 10000000 Correct answer.
  4. 11111111

Explanation

With 8 bits, the two's complement range is -128 to +127, so -128 has no positive counterpart. Its representation is 10000000: the sign bit is 1 and the remaining value corresponds to 128 in the special two's complement encoding.

Question 21Overview of Computers, Input/Output and Memory Devices, Data Representation

In the basic organisation of a computer, which bus carries the address of the memory location or I/O port that the CPU wants to access?

  1. Control bus
  2. Address bus Correct answer.
  3. Data bus
  4. Power bus

Explanation

The address bus is a one-way bus from the CPU to memory and I/O devices, and it selects the particular location to be read or written. The data bus carries the actual contents transferred, and the control bus carries timing and control signals such as read and write. A power bus supplies electrical power and is not part of this logical bus structure.

Question 22Overview of Computers, Input/Output and Memory Devices, Data Representation

A computer's main memory must be refreshed continuously because each cell stores a bit as charge on a capacitor. Which memory technology is being described?

  1. DRAM Correct answer.
  2. Flash memory
  3. SRAM
  4. Mask ROM

Explanation

Dynamic RAM (DRAM) uses a capacitor in each cell; the charge leaks away, so the memory controller refreshes the cells periodically. SRAM holds each bit in a flip-flop and does not need refreshing. Mask ROM and flash memory are non-volatile and retain their contents without refresh cycles.

Question 23Overview of Computers, Input/Output and Memory Devices, Data Representation

A bank uses an input device to read the special characters printed along the bottom of cheques using magnetically charged ink. Which device performs this task?

  1. Optical mark reader
  2. Light pen
  3. Barcode scanner
  4. MICR reader Correct answer.

Explanation

MICR stands for Magnetic Ink Character Recognition, and a MICR reader detects characters printed in magnetic ink, which is the standard method used for cheque processing. An optical mark reader senses pencil marks in fixed positions, a barcode scanner reads printed bars, and a light pen is a pointing device.

Question 24Overview of Computers, Input/Output and Memory Devices, Data Representation

In an 8-bit excess-128 representation, what is the binary pattern for the decimal number -3?

  1. 01111101 Correct answer.
  2. 11111101
  3. 01111100
  4. 10000011

Explanation

In excess-128 notation, a value is stored as its true value plus 128. For -3 the stored integer is -3 + 128 = 125, and 125 in 8-bit binary is 01111101. This bias maps 00000000 to -128 and 11111111 to +127.

Question 25Problem Solving Methodology (algorithms, flowcharts, pseudocode)

According to the problem-solving methodology followed in II PUC Computer Science, which activity is taken up immediately after the problem has been understood and analysed?

  1. Designing the algorithm to solve the problem Correct answer.
  2. Coding the program in a high-level language
  3. Compiling the source code into machine code
  4. Debugging the compiled program for syntax errors

Explanation

The methodology runs from problem definition and analysis to algorithm design, and only then to coding, compiling and testing. The algorithm is the language-independent plan of the solution, so it must be ready before any program statement is written. Writing code before designing the logic usually produces unstructured programs that are hard to debug.

Question 26Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Which symbol is drawn in a flowchart to represent an operation such as READ a value or PRINT a result?

  1. Diamond
  2. Parallelogram Correct answer.
  3. Rounded oval
  4. Rectangle

Explanation

In standard flowchart notation the parallelogram is the input/output box and holds statements such as READ n or PRINT sum. The rectangle is the process box for assignments and calculations, the diamond is the decision box, and the rounded oval marks the terminal points START and STOP.

Question 27Problem Solving Methodology (algorithms, flowcharts, pseudocode)

How is pseudocode best described in the context of algorithm design?

  1. A graphical diagram showing the flow of control in a program
  2. A structured English-like description of an algorithm's logic Correct answer.
  3. A set of syntax rules that every high-level programming language must strictly follow
  4. A machine-level code produced from a source program by a compiler

Explanation

Pseudocode uses structured English statements, keywords such as READ, IF and WHILE, and indentation to show the logic of an algorithm without following the syntax of any particular language. Because it is not tied to a language, it can later be translated into C, Python or any other language. It is never compiled, and it does not enforce strict syntactic rules.

Question 28Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Study the pseudocode: SET i = 1 SET sum = 0 WHILE i <= 5 sum = sum + i * i i = i + 1 ENDWHILE PRINT sum What value is printed?

  1. 55 Correct answer.
  2. 30
  3. 15
  4. 25

Explanation

The loop adds the square of each value of i from 1 to 5, so the running total becomes 1, 5, 14, 30 and finally 55. After the fifth addition i becomes 6 and the WHILE condition fails. The printed value is therefore 55.

Question 29Problem Solving Methodology (algorithms, flowcharts, pseudocode)

How many times is the body of the following loop executed? FOR i = 1 TO 10 STEP 3 PRINT i NEXT i

  1. 4 Correct answer.
  2. 3
  3. 5
  4. 10

Explanation

With STEP 3 the control variable takes the values 1, 4, 7 and 10, after which the next value 13 exceeds the limit 10 and the loop stops. Each iteration advances i by 3, so the body runs four times, not ten times.

Question 30Problem Solving Methodology (algorithms, flowcharts, pseudocode)

A pseudocode algorithm must interchange the values held in two variables x and y without using any additional variable. Which sequence of statements does this correctly?

  1. x = y y = x x = y
  2. x = x + y y = x + y x = x - y
  3. x = x + y y = x - y x = x - y Correct answer.
  4. x = x - y y = y - x x = x + y

Explanation

Taking x = 5 and y = 9, the first statement makes x = 14, the second makes y = 14 - 9 = 5, and the third makes x = 14 - 5 = 9, so the values are interchanged. The method works because the sum is stored temporarily inside x and is then reduced by each original value in turn. Any extra variable, such as temp, is unnecessary here.

Question 31Problem Solving Methodology (algorithms, flowcharts, pseudocode)

One step of an algorithm reads: "Multiply the total by a convenient number." Different users may choose different numbers for this step. Which characteristic of a good algorithm is violated?

  1. Finiteness
  2. Input
  3. Definiteness Correct answer.
  4. Effectiveness

Explanation

Definiteness requires every step to be clear, precise and unambiguous, so that the action to be performed is exactly the same for every user. The phrase "a convenient number" leaves the operation open to different interpretations, which is precisely what definiteness forbids. Finiteness, input and effectiveness deal with termination, data supplied and simplicity of steps, not with ambiguity of wording.

Question 32Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Trace the pseudocode: SET marks = 45 IF marks >= 60 THEN grade = "First" ELSE IF marks >= 35 THEN grade = "Pass" ELSE grade = "Fail" ENDIF PRINT grade What is printed?

  1. First
  2. Pass Correct answer.
  3. Nothing is printed
  4. Fail

Explanation

The first condition 45 >= 60 is false, so control moves to the ELSE IF part, where 45 >= 35 is true and grade becomes "Pass". The final ELSE is skipped because a true branch has already been executed. Nested selection always executes at most one branch of the chain.

Question 33Problem Solving Methodology (algorithms, flowcharts, pseudocode)

A flowchart contains the steps: SET x = 12, SET y = 18; if x > y then big = x otherwise big = y; while big <= 36, repeatedly add 6 to big; finally PRINT big. What is printed?

  1. 18
  2. 42 Correct answer.
  3. 36
  4. 24

Explanation

Since 12 > 18 is false, big is set to 18. The loop then adds 6 while the value does not exceed 36, giving 24, 30, 36 and finally 42, at which point the condition 42 <= 36 fails. The printed value is therefore 42.

Question 34Problem Solving Methodology (algorithms, flowcharts, pseudocode)

How many times is the assignment count = count + 1 executed in the following pseudocode? SET count = 0 FOR i = 1 TO 4 FOR j = i TO 4 count = count + 1 NEXT j NEXT i PRINT count

  1. 16
  2. 7
  3. 12
  4. 10 Correct answer.

Explanation

The inner loop starts at j = i, so it runs 4 times when i = 1, then 3, 2 and 1 times for the later values of i. The total is 4 + 3 + 2 + 1 = 10, which is also the value printed as count. The inner loop is not fixed at four repetitions because its lower limit depends on the outer variable.

Question 35Problem Solving Methodology (algorithms, flowcharts, pseudocode)

In a flowchart, which symbol is used to show the start and the end of an algorithm?

  1. A rectangle
  2. A diamond
  3. A parallelogram
  4. An oval or rounded rectangle Correct answer.

Explanation

The oval or rounded rectangle is the terminator symbol. It contains words such as START or STOP and has no processing role; it only marks where execution begins and where it ends.

Question 36Problem Solving Methodology (algorithms, flowcharts, pseudocode)

A procedure that may keep running forever without producing a result is not accepted as an algorithm because it lacks which characteristic?

  1. Input
  2. Finiteness Correct answer.
  3. Effectiveness
  4. Definiteness

Explanation

Finiteness requires an algorithm to terminate after a finite number of steps. A procedure that can run indefinitely has no guaranteed result, so it fails this property even when every individual step is clear and executable.

Question 37Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Trace the following pseudocode: SET n = 47 SET d = 5 SET q = 0 WHILE n >= d n = n - d q = q + 1 ENDWHILE PRINT q, n What is printed?

  1. 9 and 5
  2. 10 and 2
  3. 9 and 2 Correct answer.
  4. 8 and 7

Explanation

The loop repeatedly subtracts 5 from n and increases q by 1. After nine subtractions q becomes 9 and n becomes 2, and the loop stops because 2 is less than 5. The printed output is therefore 9 and 2.

Question 38Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Study the pseudocode: SET total = 0 FOR i = 1 TO 6 IF i MOD 2 = 0 THEN total = total + i ENDIF NEXT i PRINT total What value is printed?

  1. 6
  2. 12 Correct answer.
  3. 21
  4. 9

Explanation

The condition i MOD 2 = 0 is true only for even values of i. The values added are therefore 2, 4 and 6, giving a total of 12. The odd values 1, 3 and 5 are skipped by the selection statement.

Question 39Problem Solving Methodology (algorithms, flowcharts, pseudocode)

A decision symbol in a flowchart tests a condition. How many outgoing paths does it normally have, and how are those paths labelled?

  1. Two, usually labelled Yes and No Correct answer.
  2. Three, labelled True, False and Stop
  3. One, labelled with the condition itself
  4. As many as the number of variables used in the condition

Explanation

A decision symbol has one entry and two exits, one taken when the condition is true and the other when it is false. These exits are commonly labelled Yes and No, or True and False, so that each possible result leads to a different path.

Question 40Problem Solving Methodology (algorithms, flowcharts, pseudocode)

Trace the pseudocode: SET count = 0 FOR i = 1 TO 3 FOR j = i TO 4 count = count + 1 NEXT j NEXT i PRINT count How many times is count increased in total?

  1. 12
  2. 7
  3. 9 Correct answer.
  4. 10

Explanation

The inner loop starts at j = i and runs up to 4. For i = 1 it executes 4 times, for i = 2 it executes 3 times, and for i = 3 it executes 2 times. The total is 4 + 3 + 2 = 9.

Question 41Object-Oriented Concepts and Introduction to C++

In object-oriented programming, which feature refers to wrapping data and the functions that operate on that data into a single unit?

  1. Polymorphism
  2. Inheritance
  3. Dynamic binding
  4. Encapsulation Correct answer.

Explanation

Encapsulation is the binding of data members and the member functions that manipulate them into a single unit, the class, with access controlled by specifiers such as private and public. Inheritance, polymorphism and dynamic binding describe reuse of an existing class, one-name-many-forms behaviour and run-time resolution of calls respectively, not the wrapping of data together with functions.

Question 42Object-Oriented Concepts and Introduction to C++

Which of the following is a valid identifier in C++?

  1. total marks
  2. total_marks2 Correct answer.
  3. float
  4. 2nd_rank

Explanation

A C++ identifier must begin with a letter or an underscore and may then contain letters, digits and underscores, but never a blank space, and it must not be a keyword. Therefore 2nd_rank fails on its first character, total marks contains a space, and float is a reserved word, leaving only total_marks2 valid.

Question 43Object-Oriented Concepts and Introduction to C++

Which stream object and operator are used to display data on the standard output device in C++?

  1. cout with the >> operator
  2. cout with the << operator Correct answer.
  3. cin with the >> operator
  4. cin with the << operator

Explanation

cout is the predefined object of the ostream class connected to standard output, and the insertion operator << directs data into that stream. cin is the standard input stream and works with the extraction operator >>, so every other pairing either reverses the direction of data flow or misuses the stream object.

Question 44Object-Oriented Concepts and Introduction to C++

A private data member of a class can be used directly by

  1. any function written anywhere in the same program
  2. the class's own member functions and its friends Correct answer.
  3. only the member functions of a derived class
  4. any object of the class through the dot operator

Explanation

A private member is visible only within the class that declares it, so the member functions of that class may use it directly, and friend functions or friend classes are granted the same right. Outside code cannot reach a private member through an object, and a derived class has no access to the private members of its base class, which rules out the other choices.

Question 45Object-Oriented Concepts and Introduction to C++

Which statement about a constructor in C++ is correct?

  1. It has the same name as the class and no return type. Correct answer.
  2. It is automatically inherited by every derived class.
  3. It has the same name as the class and returns void.
  4. It must always accept at least one argument from the caller.

Explanation

A constructor is a special member function whose name is identical to the class name, and it is declared without any return type, not even void. It is invoked automatically when an object is created, may take arguments or none at all, and is not inherited by derived classes, so the remaining statements are false.

Question 46Object-Oriented Concepts and Introduction to C++

If a class declares int sum(int) and float sum(int) as two member functions, what does the compiler do?

  1. The compiler reports an error, because return type alone cannot distinguish overloads. Correct answer.
  2. They form a valid pair of overloaded functions in the class.
  3. The compiler silently keeps the one that is declared last.
  4. They are treated as one function that has two return types.

Explanation

Function overloading requires the functions to differ in their parameter lists, that is, in the number, type or order of parameters. The return type is not part of the signature used during overload resolution, so two functions whose parameter lists are identical but whose return types differ are a conflicting redeclaration and are rejected by the compiler.

Question 47Object-Oriented Concepts and Introduction to C++

When default arguments are used in a C++ function, which rule must be followed?

  1. Every parameter of the function must be given a default value.
  2. Default values may be given only in the definition, never the declaration.
  3. Only the first parameter of a function may have a default value.
  4. Every parameter to the right of a defaulted one must also have a default. Correct answer.

Explanation

Default arguments are supplied from right to left when a call omits trailing arguments, so once a parameter has a default value every parameter after it must also have one. If a middle parameter were defaulted while a later one was not, the compiler could not tell which argument the caller had omitted.

Question 48Object-Oriented Concepts and Introduction to C++

Consider the statements int a = 10; int &b = a; b = 25; What is the value of a afterwards?

  1. 25, because b is an alias for a Correct answer.
  2. 0, because b is left uninitialised
  3. 25 only if a is declared as static
  4. 10, because b receives a copy of a

Explanation

A reference variable is an alternative name for an existing object rather than a separate storage location, so after int &b = a; both b and a designate the same memory. Assigning 25 through b therefore changes the value seen through a, and this aliasing works whatever the storage duration of a happens to be.

Question 49Object-Oriented Concepts and Introduction to C++

A class Area declares the constructors Area(int r) and Area(int r, int h = 1). The statement Area a(5); is then written. What happens?

  1. The two-argument constructor is called with h set to 1, because the default value is applied.
  2. The one-argument constructor is called, since an exact match outranks a default argument.
  3. The compiler reports an ambiguity, because both constructors accept one argument. Correct answer.
  4. The call is resolved in favour of the constructor declared first in the class.

Explanation

Both Area(int) and Area(int, int = 1) are viable for a call with a single int argument, and for that argument each provides an exact match. C++ overload resolution contains no tie-breaker that prefers the candidate which needs no default argument, nor one based on declaration order, so the call is ambiguous and the compiler rejects the program.

Question 50Object-Oriented Concepts and Introduction to C++

A local array of three objects is created as Sample s[3]; and both the constructor and the destructor of Sample print a message. In what order are the messages printed?

  1. Three constructor messages in order, then three destructor messages in the same order.
  2. Three constructor messages in order, then three destructor messages in reverse order. Correct answer.
  3. One constructor message and one destructor message, since the array uses a single object.
  4. Constructor and destructor messages alternate for each element of the array.

Explanation

The elements of a local array are constructed one by one in increasing index order and all of them stay alive until the array's scope ends. Destruction then happens in the reverse of construction, so the destructors print for s[2], s[1] and s[0] after the three constructor messages for s[0], s[1] and s[2].