---
title: "Display N characters using default arguments"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:n-chars-default-args-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/n-chars-default-args-program
site: BCT Engineering
---

# Display N characters using default arguments

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2073 Shrawan, 4 marks
- Video: [2080 - Group A - Question 1 - CSIT - Object Oriented Programming](https://www.youtube.com/watch?v=3t8HUK0bX-s) — Quantum & Lotus
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/n-chars-default-args-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

Write a program to display N number of characters by using default arguments for both parameters. Assume that the function takes two arguments: one character to be printed and the other number of characters to be printed.

## Short answer

Define a function display(char ch = '*', int n = 10) that loops n times printing ch. Calls display() show 10 stars; display('#') prints 10 #; display('A',5) prints 5 A's. Arguments are matched left to right; cannot skip ch to give only n.

## Answer

Both parameters get default values: the character defaults to `'*'` and the count defaults to `10`.

```cpp
#include <iostream>
using namespace std;

void display(char ch = '*', int n = 10)
{
    for (int i = 0; i < n; i++)
        cout << ch;
    cout << endl;
}

int main()
{
    display();          // uses both defaults
    display('#');       // uses default count: ##########
    display('A', 5);    // no defaults used: AAAAA
    return 0;
}
```

#### Output

```text
**********
##########
AAAAA
```

#### Explanation

- `display()` : both arguments omitted, so `ch = '*'`, `n = 10`.
- `display('#')` : only the first is given; `n` takes 10.
- `display('A', 5)` : both given; defaults are ignored.
- Arguments are matched left to right, so we cannot skip `ch` and give only `n`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. display() prints?
   A. Ten asterisks
   B. Nothing
   C. One asterisk
   D. Ten #
   Answer: A. Defaults * and 10. (Key point: Defaults)

2. display('#') prints?
   A. Five #
   B. Ten #
   C. One #
   D. Ten *
   Answer: B. n defaults 10. (Key point: One arg)

3. Can you skip ch and give only n?
   A. Yes
   B. Only with ref
   C. No; arguments are matched left to right
   D. Yes if int
   Answer: C. Left to right. (Key point: Skipping)

## Related in Object Oriented Programming

- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×
- [Default arguments and their syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-define-syntax) — Object Oriented Programming, chapter 3, asked 2×
- [Default arguments in class template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-default-args) — Object Oriented Programming, chapter 9
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1

## Related in other subjects

- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [Display characters between given ranges](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acharacters-between-ranges) — Computer Programming, chapter 5
- [Convert case based on first character](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswap-case-first-char-word) — Computer Programming, chapter 7
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
