Ruby Programming Questions and Answers – Assignment Operators

«
»

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Assignment Operators”.

1. Which of the following is a valid assignment operator?
a) +=
b) -=
c) *=
d) All of the mentioned
View Answer

Answer: d
Explanation: Assignment operators are +=, -=, *=, /=, **=.
advertisement

2. What does the **= assignment operator do?
a) Multiplies the value twice
b) It is used as exponent like 2**3=8
c) It is the multiplication operator.
d) None of the mentioned
View Answer

Answer: b
Explanation: a**=2 means a to the power of two.

3. What is the output of the given code?

counter = 2
while counter < 68
  puts counter
  counter**=2
  end

a) 2 4 16 64
b) 2 4 16
c) 2 4 16 256
d) None of the mentioned
View Answer

Answer: b
Explanation: The counter will increment by the power of two.

Output:
2
4
16

4. What is the output of the given code?

advertisement
counter = 1
while counter < 11
  puts counter
  counter+=1
  end

a) 1 2 3 4 5
b) 1…10
c) 1..10
d) None of the mentioned
View Answer

Answer: c
Explanation: 1..10 inclusive 10 also. The counter will increment by one.

Output:
1
2
3
4
5
6
7
8
9
10

5. Ruby does not support ++ operator, it only supports += operator.
a) True
b) False
View Answer

Answer: a
Explanation: Ruby does not support ++ or — operator. It only supports += or -= operator.

6. What is the output of the given code?

counter = 100
while counter > 0
  puts counter
  counter/=5
  end

a) 100 20 4
b) 100 20 5
c) 100..5
d) None of the mentioned
View Answer

Answer: a
Explanation: First counter will be 100 then after division it will become 20 then again it will become 4 and then it will come out of the loop.

advertisement
Output: 
100
20
4

7. What is the output of the given code?

counter = 100
while counter > 0
  puts counter
  counter-=25
  end

a) 100 75 50 25
b) 100 25 5
c) 100..5
d) None of the mentioned
View Answer

Answer: a
Explanation: The counter value will decrement by 25.

Output: 
100
75
50
25

8. What is the output of the given code?

counter = -50
while counter <0
  puts counter
  counter+=10
  end

a) 100 75 50 25
b) -50 -40 -30 -20 -10
c) 100..5
d) None of the mentioned
View Answer

Answer: b
Explanation: The counter value will decrement by 10 till the counter value reaches zero.

advertisement
Output: 
-50
-40
-30
-20
-10

9. The given two expression means the same.

counter=counter+1 and counter++

a) True
b) False
View Answer

Answer: a
Explanation: Both the expression increments the value by 1.

10. What is the output of the given code?

a = 22.5
while a >11.5
  puts a
  a-=3.5
  end

a) 22.5 19.0 15.5 12.0
b) 22.5 11.5
c) 100..5
d) None of the mentioned
View Answer

Answer: b
Explanation: The counter value will decrement by 3.5 till the counter value reaches 11.5.

Output: 
22.5
19.0
15.5
12.0

11. What is the output of the given code?

a = 5
b=10
while a <10 && b<20
  puts a+b
  a+=2
  b+=2
  end

a) 10 20
b) 15 19 23
c) 15 16 17 18 19 20
d) None of the mentioned
View Answer

Answer: b
Explanation: The counter value will increment by two till a<10 and b<20.
Output: 
15 
19 
23

12. What is the output of the given code?

a = 5
b=10
while a <10 && b<20
  puts a*b
  a+=2
  b+=2
  end

a) 10 20
b) 15 19 23
c) 50 84 126
d) None of the mentioned
View Answer

Answer: c
Explanation: The counter value will increment by two till a<10 and b<20 and multiply a and b.
Output: 
50
84
126

13. What is the output of the given code?

 a= 5
b=10
while a <10 && b<20
  puts a-b
  a+=2
  b+=2
  end

a) 10 20
b) 15 19 23
c) -5 -5 -5
d) None of the mentioned
View Answer

Answer: c
Explanation: Even if both the values are incremented by two the difference remains the same.

Output: 
-5
-5
-5

14. What is the output of the given code?

 a = 5
b=10
while a <10 || b<20
  puts a*b
  a+=2
  b+=2
  end

a) 10 20
b) 50 84 126 176 234
c) -5 -5 -5
d) None of the mentioned
View Answer

Answer: b
Explanation: The while will keep on executing even if only one condition is true.

Output: 
50
84
126
176
234

15. What is the output of the given code?

 a = 5
b=10
while (a <10 || b<20)&&true
  puts a*b
  a+=2
  b+=2
  end

a) 10 20
b) 50 84 126 176 234
c) -5 -5 -5
d) None of the mentioned
View Answer

Answer: b
Explanation: The while will keep on executing when both the conditions are true.

Output: 
50
84
126
176
234

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn