Docsity

WGU D335 INTRODUCTION TO PROGRAMMING IN PYTHON - PERFORMANCE ASSESSMENT (PA) 100 CODING, Exams of Nursing

Toggle sidebar
bg1
WGU D335 INTRODUCTION TO
PROGRAMMING IN PYTHON -
PERFORMANCE ASSESSMENT (PA)
100 CODING PRACTICE
QUESTIONS WITH VERIFIED
SOLUTIONS
SECTION ONE: QUESTIONS 1–100
Question 1
Problem: Write a program that takes an integer input from the user and
prints "Even" if the number is even, and "Odd" if the number is odd.
Solution:
python
num = int(input())
if num % 2 == 0:
print("Even")
else:
print("Odd")
RATIONALE: The modulus operator % returns the remainder. If num
% 2 == 0, the number is evenly divisible by 2, making it even.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33

Partial preview of the text

Download WGU D335 INTRODUCTION TO PROGRAMMING IN PYTHON - PERFORMANCE ASSESSMENT (PA) 100 CODING and more Exams Nursing in PDF only on Docsity!

WGU D335 INTRODUCTION TO

PROGRAMMING IN PYTHON -

PERFORMANCE ASSESSMENT (PA)

100 CODING PRACTICE

QUESTIONS WITH VERIFIED

SOLUTIONS

SECTION ONE: QUESTIONS 1– 100

Question 1 Problem : Write a program that takes an integer input from the user and prints "Even" if the number is even, and "Odd" if the number is odd. Solution : python num = int(input()) if num % 2 == 0: print("Even") else: print("Odd") RATIONALE: The modulus operator % returns the remainder. If num % 2 == 0, the number is evenly divisible by 2, making it even.

Question 2 Problem : Write a program that takes two integer inputs and prints their sum, difference, product, and quotient (as a float). Solution : python a = int(input()) b = int(input()) print("Sum:", a + b) print("Difference:", a - b) print("Product:", a * b) print("Quotient:", a / b) RATIONALE: Basic arithmetic operations are performed in Python. Division / always returns a float. Output formatting must match exactly as shown. Question 3 Problem : Write a program that takes a string input and prints it in reverse order. Solution : python text = input() print(text[::-1]) RATIONALE: The slice [::-1] starts at the end and moves backwards with a step of - 1, effectively reversing the string.

Question 6 Problem : Write a program that takes a string input and counts the number of vowels (a, e, i, o, u) in it. Solution : python text = input().lower() vowels = "aeiou" count = 0 for char in text: if char in vowels: count += 1 print("Vowels:", count) RATIONALE: The string is converted to lowercase to handle uppercase inputs. Each character is checked against the vowel set. Question 7 Problem : Write a program that takes an integer n and prints all numbers from 1 to n on a single line, separated by spaces. Solution : python n = int(input()) for i in range(1, n + 1): print(i, end=" ") RATIONALE: range(1, n+1) generates numbers from 1 to n inclusive. end=" " keeps output on one line with spaces.

Question 8 Problem : Write a program that takes three integer inputs and prints them in ascending order. Solution : python a = int(input()) b = int(input()) c = int(input()) nums = [a, b, c] nums.sort() print("Sorted:", nums[0], nums[1], nums[2]) RATIONALE: The sort() method arranges list elements in ascending order. Question 9 Problem : Write a program that calculates the factorial of a number entered by the user. Solution : python n = int(input()) factorial = 1 for i in range(1, n + 1): factorial *= i print("Factorial:", factorial)

RATIONALE: sum() calculates the total, len() gets the count. Division returns a float. Question 12 Problem : Write a program that checks if a string is a palindrome. Solution : python text = input() if text == text[::-1]: print("Palindrome") else: print("Not a palindrome") RATIONALE: A palindrome reads the same forwards and backwards. text[::-1] reverses the string. Question 13 Problem : Write a program that takes two numbers and prints all numbers between them (inclusive) that are divisible by 3. Solution : python a = int(input()) b = int(input()) start = min(a, b) end = max(a, b) for i in range(start, end + 1):

if i % 3 == 0: print(i) RATIONALE: min() and max() ensure the range is correct regardless of input order. Question 14 Problem : Write a program that takes a sentence and counts the number of words in it. Solution : python sentence = input() words = sentence.split() print("Words:", len(words)) RATIONALE: split() divides the string at spaces, returning a list of words. len() counts them. Question 15 Problem : Write a program that takes an integer and prints its multiplication table up to 10. Solution : python n = int(input()) for i in range(1, 11): print(n, "x", i, "=", n * i)

RATIONALE: % 10 extracts the last digit, // 10 removes it. The loop continues until all digits are processed. Question 18 Problem : Write a program that takes a list of strings and prints them sorted alphabetically. Solution : python words = [] for i in range(5): words.append(input()) words.sort() for word in words: print(word) RATIONALE: sort() arranges strings alphabetically by default. Question 19 Problem : Write a program that takes a number and prints whether it is positive, negative, or zero. Solution : python num = int(input()) if num > 0: print("Positive") elif num < 0:

print("Negative") else: print("Zero") RATIONALE: The conditional statements check the value against 0. Question 20 Problem : Write a program that takes two strings and checks if they are anagrams. Solution : python str1 = input() str2 = input() if sorted(str1) == sorted(str2): print("Anagrams") else: print("Not anagrams") RATIONALE: sorted() returns a list of characters in alphabetical order. If both strings have the same sorted list, they are anagrams. Question 21 Problem : Write a program that takes a list of integers and removes all duplicates. Solution : python nums = []

print(sentence.title()) RATIONALE: title() capitalizes the first letter of each word. Question 24 Problem : Write a program that prints the Fibonacci sequence up to the nth term. Solution : python n = int(input()) a, b = 0, 1 for i in range(n): print(a) a, b = b, a + b RATIONALE: The Fibonacci sequence starts with 0 and 1. Each subsequent number is the sum of the two preceding ones. Question 25 Problem : Write a program that takes a string and counts the occurrences of each character. Solution : python text = input() char_count = {} for char in text: if char in char_count:

char_count[char] += 1 else: char_count[char] = 1 print(char_count) RATIONALE: A dictionary stores each character as a key and its count as the value. Question 26 Problem : Write a program that takes three side lengths and determines if they form a valid triangle. Solution : python a = float(input()) b = float(input()) c = float(input()) if a + b > c and a + c > b and b + c > a: print("Valid triangle") else: print("Invalid triangle") RATIONALE: The triangle inequality theorem states that the sum of any two sides must be greater than the third. Question 27 Problem : Write a program that takes a list of numbers and prints the second largest number.

Question 29 Problem : Write a program that converts a decimal number to binary. Solution : python num = int(input()) binary = bin(num) print(binary[2:]) RATIONALE: bin() returns a binary string prefixed with "0b". Slicing removes the prefix. Question 30 Problem : Write a program that takes a list of student scores and calculates the average, excluding the lowest and highest scores. Solution : python scores = [] for i in range(5): scores.append(int(input())) scores.sort() scores = scores[1:-1] average = sum(scores) / len(scores) print("Adjusted average: {:.2f}".format(average)) RATIONALE: Sorting the list and slicing [1:-1] removes the first and last elements (lowest and highest).

Question 31 Problem : Write a program that takes a number and prints "Prime" if it is prime, otherwise "Not prime". Solution : python n = int(input()) is_prime = True if n < 2: is_prime = False else: for i in range(2, int(n ** 0.5) + 1): if n % i == 0: is_prime = False break print("Prime" if is_prime else "Not prime") RATIONALE: A prime number has no divisors other than 1 and itself. The loop only needs to check up to the square root. Question 32 Problem : Write a program that takes two strings and concatenates them with a space in between. Solution : python str1 = input() str2 = input()

t = float(input()) # Time in years A = P * (1 + r/n) ** (n * t) print("Amount: {:.2f}".format(A)) RATIONALE: The formula is applied with appropriate operator precedence. Question 35 Problem : Write a program that takes a string and checks if it contains only digits. Solution : python text = input() if text.isdigit(): print("Contains only digits") else: print("Contains non-digits") RATIONALE: isdigit() returns True if all characters in the string are digits. Question 36 Problem : Write a program that takes a list of numbers and prints their sum using a while loop. Solution : python nums = []

for i in range(5): nums.append(int(input())) total = 0 i = 0 while i < len(nums): total += nums[i] i += 1 print("Sum:", total) RATIONALE: A while loop iterates through the list using an index variable, adding each element. Question 37 Problem : Write a program that takes a sentence and prints the longest word. Solution : python sentence = input() words = sentence.split() longest = "" for word in words: if len(word) > len(longest): longest = word print("Longest word:", longest) RATIONALE: The loop keeps track of the longest word found so far.