[s1g1] Python Typecasting Cw

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Raja Marwah
R
Raja Marwah
Community Contributor
Quizzes Created: 2 | Total Attempts: 150
Questions: 17 | Attempts: 104

Settings

Read the questions very carefully and solve them in PyCharm

• 1.

• 2.

Write the output for the following lines of code var1=str(34.5+2)+str(34.5+2) print(var1)

Explanation
The output of the code is "36.536.5".

In the given code, the expression "34.5+2" is evaluated first, resulting in the float value 36.5. Then, the str() function is used to convert this float value to a string. The same calculation is repeated for the second part of the expression. Finally, the two resulting strings are concatenated together, resulting in "36.536.5".

Rate this question:

• 3.

A="12.5" #line 1 b="13.5" #line2 c=??? <-------- line 3 print(c) In the above program complete line 3 with instruction using only typecasting command i.e int(), str(),float() and mathematical operators such that the output is 26

Explanation
The given program requires the value of c to be 26. To achieve this, we need to convert the string values of a and b into floating-point numbers using the float() function. Then, we can add the two floating-point numbers together. Finally, we can convert the result into an integer using the int() function to get the desired output of 26.

Rate this question:

• 4.

Write the output for the following lines of code var1=5 print(float(var1))

Explanation
The output of the code is 5.0 because the variable var1 is assigned the value 5. When the print function is called with float(var1), it converts the integer value 5 to a floating-point number, which is represented as 5.0.

Rate this question:

• 5.

Write the output for the following lines of code var1="5" print(float(var1))

Explanation
The code declares a variable named var1 and assigns the string value "5" to it. The float() function is then used to convert this string value to a floating-point number. The print() function is used to display the converted value, which is 5.0.

Rate this question:

• 6.

Write the output for the following lines of code var1="5" var2="2" print(float(var1/var2))

Explanation
The given code tries to divide two variables var1 and var2, which are both strings. However, the division operator (/) is not defined for strings in Python. Therefore, when the code tries to execute float(var1/var2), it raises a TypeError and outputs "error".

Rate this question:

• 7.

Write the output for the following lines of code var1="5/2" print(int(var1))

Explanation
The code tries to convert the string "5/2" into an integer using the int() function. However, since "5/2" is not a valid integer, it will result in a ValueError and raise an error.

Rate this question:

• 8.

Write the output for the following lines of code var1=str(int("2"))+"ab" print(var1)

Explanation
The code first converts the string "2" into an integer using the int() function. Then, it converts the integer back into a string using the str() function. Finally, it concatenates the string "ab" to the end of the resulting string "2". Therefore, the output of the code is "2ab".

Rate this question:

• 9.

Write the output for the following lines of code var1=str(34+2)+str(34+2) print(var1)

Explanation
The code first calculates the sum of 34+2, which is 36. Then it converts this result to a string using the str() function. The same calculation is performed again, resulting in another 36. This result is also converted to a string. Finally, the two strings are concatenated together using the + operator. Therefore, the output is "3636".

Rate this question:

• 10.

Write the output for the following lines of code var1=str(34+2)+int(34+2) print(var1)

Explanation
The code is trying to concatenate a string and an integer using the "+" operator. However, the "int(34+2)" part of the code is trying to convert the result of the addition into an integer. Since we cannot concatenate a string and an integer directly, it results in a TypeError and throws an error.

Rate this question:

• 11.

Write the output for the following lines of code var1=int("-20"+str(20)) print(var1)

Explanation
The code first converts the string "-20" into an integer. Then it concatenates the string "20" to the integer, resulting in the string "-2020". Finally, it prints the value of the variable "var1", which is -2020.

Rate this question:

• 12.

Write the output for the following lines of code var1=int("12")//float(2) print(var1)

Explanation
The code first converts the string "12" into an integer using the int() function. Then, it performs integer division by dividing the result by the float value 2. Since integer division always returns a float result, the final output is 6.0.

Rate this question:

• 13.

A="12.5" #line 1 b="13.5" #line2 c=??? <-------- line 3 print(c) In the above program complete line 3 with instruction using only typecasting command i.e int(), str(),float() and mathematical operators such that the output is 26.0

Explanation
The given program uses typecasting to convert the strings 'a' and 'b' into floating-point numbers using float(). Then, it adds the two floating-point numbers together and converts the result into an integer using int(). Finally, it assigns the result to the variable 'c'. The output of the program will be 26.0.

Rate this question:

• 14.

a="12.5" #line 1 b="13.5" #line2 c="hello" #line 3 d=??? <-------- line 4 print(d) In the above program complete line 4 with instruction using only typecasting command i.e int(), str(),float() and mathematical operators such that the output is 26.0hello

Explanation
The given code snippet assigns the values "12.5" to variable a, "13.5" to variable b, and "hello" to variable c. The task is to complete line 4 in such a way that the output is "26.0hello". To achieve this, the code uses typecasting to convert the values of a and b from strings to floats using the float() function. Then, it adds the converted values of a and b, and converts the result back to a string using the str() function. Finally, it concatenates the string result with the value of c to get the desired output.

Rate this question:

• 15.

A=25 #line 1 b=30 #line 2 c="hello" #line 3 d=??? <-------- line 4 print(d)In the above program complete line 4 using the variables a,b,c with typecasting command i.e int(), str(),float() and mathematical operators such that the output is 2530hello

Explanation
The correct answer is d=str(a)+str(b)+c because it uses the str() function to convert the variables a and b into strings, and then concatenates them with the variable c to create the desired output "2530hello".

Rate this question:

• 16.

A=0.25 #line 1 b=0.75 #line2 c="c" #line 3 d=??? <-------- line 4 print(d) In the above program replace line 2 with instruction using typecasting command i.e int(), str(),float() such that the output is 1c

Explanation
The correct answer is d=str(int(a+b))+c. In this program, the variables a and b are assigned the values 0.25 and 0.75 respectively. The variable c is assigned the string "c". The variable d is assigned the value of the expression str(int(a+b))+c. Here, the values of a and b are added and converted to an integer using the int() function. Then, the result is converted to a string using the str() function. Finally, the string "c" is concatenated with the result using the + operator. The output will be "1c".

Rate this question:

• 17.

a=12 #line 1 b=12 #line2 c="/" #line 3 d=??? <-------- line 4 print(d) In the above program replace line 4 with instruction using typecasting command i.e int(), str(),float() and mathematical operators such that the output is 12/12

Explanation
The correct answer is d=str(a)+c+str(b). In this program, the variable d is assigned the value of str(a)+c+str(b). The str(a) converts the integer value of a to a string, c is already a string containing the division operator "/", and str(b) converts the integer value of b to a string. When these values are concatenated using the "+" operator, the result is the string "12/12".

Rate this question: