# The Joy of Computing using Python Week 4 Assignment 4 | NPTEL | [Jan 2023]

## Which of the following statements are true with regards to magic square?

a. The sum of each row should be m.
b. The sum of each column should be m.
c. The sum of each diagonal should be m.
d. None of the above.

a. The sum of each row should be m.
b. The sum of each column should be m.
c. The sum of each diagonal should be m.

- In a magic square each row, column and diagonal have the same sum.

## Which of the following statements hold true about N in the magic square? N denotes the number of rows and columns in the square.

a. N should be even.
b. N should be odd.
c. N can be even or odd.
d. N can take any value.

b. N should be odd.
‘N’ can take any number but it should be odd it should not be even

## Which of the following statements are true regarding the Magic Squares? (N = Number of rows or columns)

a. A Magic Square is always a square matrix.
b. A Magic Square can or cannot be a square matrix.
c. The Sum of each row and each column is N(N+1)/2
d. The Sum of each row and each column is N(N^2+1 )/2.

a. A Magic Square is always a square matrix.
d. The Sum of each row and each column is N(N^2+1 )/2.

M = n(n^2+1)/2
A magic square of order n is an arrangement of n2 numbers,

## What will be the output of the following code?

```'''
This is a sentence
'''```
a. This is a sentence.
b. Error
c. No output
d. The program will not run

c. No output
Triple quotes, are docstrings, or multi-line docstrings.

## Which of the following operator is used to raise the exponent to a number?

a. ^
b. *
c. **
d. ***

c. **
The exponentiation operator is used to raise a number to a power or exponent in most programming languages. In Python, for example, the double asterisk operator (**), is used for exponentiation.

Here's an example of how you can use the exponentiation operator to raise a number to a power:
example
# 2 raised to the power of 3
result = 2 ** 3
print(result)
# Output: 8

In this example, 2 is the base and 3 is the exponent, so 2 ** 3 returns the result 8.

## Suppose there is a movie with 3 letters, how many combinations of names are possible?

a. 26 b. 676 c. 17576 d. 456976

c. 17576
If a movie name consists of 3 letters, and each letter can be chosen from 26 possible options (assuming only English alphabets are used), then the total number of possible combinations of names is:
26 x 26 x 26 = 17576

## What should be the value of a, b, c, d respectively?

```6 a 8
b 5 c
2 d 4
```
a. 1,3,9,7
b. 9,3,7,1
c. 1,7,3,9
d. 7,3,9,1

c. 1,7,3,9
In a magic square each row, column and diagonal have the same sum. for example in given matrix first row sum is 6 + 1 + 8 = 15,where a = 1.

```L1 = ['harry potter', 'matrix', 'spiderman', 'avengers', 'john wick']
L2 = ['drishyam', 'spiderman', 'bahubali', 'dhoom', 'race', 'matrix']

L  = []

for i in range(len(L1)):

flag = 0

for j in range(len(L2)):

if(L1[i] == L2[j]):
flag = 1
break
else:
flag = 0

if(flag == 0):
L.append(L1[i])

print(L)```
a. Print unique movies of list L1
b. Print unique movies of list L2
c. Print unique movies of list L1 and L2
d. Shows an error

a. Print unique movies of list L1

```L1 = ['harry potter', 'matrix', 'spiderman', 'avengers', 'john wick']
L2 = ['drishyam', 'spiderman', 'bahubali', 'dhoom', 'race', 'matrix']

L  = []

for i in range(len(L1)):

flag = 0

for j in range(len(L2)):

if(L1[i] == L2[j]):
flag = 1
break
else:
flag = 0

if(flag == 0):
L.append(L1[i])

print(L)```

Output: ['harry potter', 'avengers', 'john wick']

## What will be the output of the following code?

```for i in range(5,20):
if(i%5 == 0):
print(i**2)
```
a. Print all perfect squares with square roots between 5-20 and divisible by 5.
b. Print all perfect squares with square roots between 5-20 and not divisible by 5.
c. Print all perfect squares with square roots between 5-19 and not divisible by 5.
d. Print all perfect squares with square roots between 5-19 and divisible by 5.

d. Print all perfect squares with square roots between 5-19 and divisible by 5.

The for loop starts with the keyword for, followed by a loop variable i, and the range function, which generates a sequence of numbers from 5 to 19. So the loop variable i will take on the values 5, 6, 7, ..., 19, in turn.

The if statement checks whether the value of i is a multiple of 5 (i.e., i is divisible by 5 with no remainder), by using the modulo operator %. If i is a multiple of 5, then the code inside the if block will be executed.

The print statement inside the if block calculates the square of i (i.e., i raised to the power of 2) using the exponentiation operator **, and prints the result to the console.

The for loop then continues with the next value of i, until it has looped over all the values in the range object.

So overall, this code will print out the squares of all the numbers in the range from 5 to 19 that are multiples of 5, i.e., the numbers 25, 100, and 225.

## A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number as the sum of its divisors 1,2,3 is equal to 6. Which function will return True if the number is a perfect number?

a.
```def perfect_number(num):
ans=0
for i in range(1,num):
if (num%i==0):
ans = ans + i
if(ans==num):
return True
else:
return False```
b.
```def perfect_number(num):
ans=0
for i in range(1,num):
if (num%i==0):
ans += i
if(ans==num):
return False
else:
return True```
c.
```def perfect_number(num):
ans=0
for i in range(3,num):
if (num%i==0):
ans = ans + i
if(ans==num):
return True
else:
return False```
d.
```def perfect_number(num):
ans=0
for i in range(1,num):
if (num%i==0):
ans = ans + i
if(ans!=num):
return True
else:
return False```

a=Option a is correct One

The given code defines a function perfect_number which takes a single argument num as input. The function aims to check whether num is a perfect number or not.

A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). For example, 6 is a perfect number because its proper divisors are 1, 2, and 3, and 1+2+3=6.

The function first initializes a variable ans to 0. It then loops through all the integers from 1 to num-1 using the range() function. For each integer i in this range, it checks if num is divisible by i using the modulo operator %. If it is, then i is a proper divisor of num, so the code adds i to the running sum ans.

After the loop has finished, the code checks if the final value of ans is equal to num. If it is, then num is a perfect number, so the function returns True. Otherwise, the function returns False.

In summary, the perfect_number function checks whether the input num is a perfect number or not, by finding the sum of all its proper divisors and checking whether this sum is equal to num.

## The Joy of Computing using Python - Course | NPTEL - All asignment

Disclaimer:
"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."