Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam PCAP All Questions

View all questions & answers for the PCAP exam

Exam PCAP topic 1 question 61 discussion

Actual exam question from Python Institute's PCAP
Question #: 61
Topic #: 1
[All PCAP Questions]

What is the expected output of the following code?

  • A. 21
  • B. 12
  • C. 3
  • D. none
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
46a1b89
1 month ago
I have no idea why people give here wrong answer and they make himself very sure with his mistakes..the answer of this question is D output 3
upvoted 1 times
...
alopezme
8 months ago
There is no need for an else, if n != 1 it will return the n + f (n-1) So correct answer is 3
upvoted 1 times
...
blaze056
10 months, 3 weeks ago
Selected Answer: C
executed and verified
upvoted 1 times
...
TheFivePips
11 months, 3 weeks ago
Another example of where blind copy and paste makes this questions boarderline unanswerable.
upvoted 3 times
...
swyyuen
1 year, 1 month ago
def f(n): if n == 1: return 1 return n + f(n-1) print(f(2)) Answer will be 3 ------------------------------------ def f(n): if n == 1: return 1 return n + f(n-1) Answer will be None
upvoted 1 times
...
emanuelcar990
1 year, 3 months ago
The answer is None def f(n): if n == 1: return 1 return n + f(n-1) print(f(2)) test the code and result = None because dont have Else: on if
upvoted 1 times
...
owenmagas
1 year, 3 months ago
The answer to the problem is 3
upvoted 1 times
...
CC_DC
1 year, 4 months ago
Throw that code as-is and what are the results? D is the correct answer.
upvoted 2 times
...
Ello2023
1 year, 4 months ago
Selected Answer: D
This is the correct code that prints 3 as the answers def f (n): if n == 1: return 1 else: return n + f (n-1) print (f(2))
upvoted 2 times
...
Ello2023
1 year, 4 months ago
Selected Answer: D
D is correct
upvoted 1 times
...
Ello2023
1 year, 4 months ago
when you run this snippet on a compiler it prints out None. Even if the indentation is corrected you can not have 2 results in one function unless it is separated by an else like below def get_absolute_value(num): if num < 0: return -num else: return num result = get_absolute_value(-5) print(result) # Output: 5
upvoted 1 times
...
david0001
1 year, 9 months ago
It's a recursive function that adds up all the numbers from n, n-1, n-2 ,...,1. So, given n=2, and assuming the indentation is correct, the result will be: 2 + 1 = 3.
upvoted 1 times
...
rotimislaw
2 years ago
Selected Answer: C
C assuming the indentation is correct (there's no option for "the code causes runtime error", so I guess the indentation is correct)
upvoted 3 times
...
Ram5678
2 years, 1 month ago
The answer is C if the indentation is assumed to be correct.
upvoted 2 times
...
JO5H
2 years, 1 month ago
Selected Answer: C
Its not properly indented so it should return errors but since that is not an option then the answer is C if indentation is assumed to be correct
upvoted 2 times
...
alfonsocav1982
2 years, 2 months ago
The answer in this case is None as there is no "else" specified, so that works only when n==1
upvoted 1 times
...
Jnanada
2 years, 3 months ago
answer should be C if identation is correct
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...