Ruby MCQs On Variables
This section focuses on "Variables" in Ruby. These Multiple Choice Questions (MCQ) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. Global variables in ruby begin with?
A. @
B. #
C. $
D. &
View Answer
Ans : C
Explanation: Global variables begin with $.
2. Default value of global variable is?
A. -1
B. nil
C. 1
D. infinite
View Answer
Ans : B
Explanation: Global variables begin with $. Uninitialized global variables have the value nil.
3. Instance variables in ruby begin with?
A. @
B. #
C. $
D. &
View Answer
Ans : A
Explanation: Instance variables begin with @.
4. Class variables in ruby begin with?
A. @
B. #
C. double @
D. ##
View Answer
Ans : C
Explanation: Class variables begin with @@
5. Default value of class variables is?
A. nil
B. infinite
C. -1
D. error
View Answer
Ans : D
Explanation: uninitialized class variable produces an error.
6. Local variables in ruby begin with?
A. @
B. _
C. *
D. #
View Answer
Ans : B
Explanation: Local variables begin with a lowercase letter or _.
7. Constants in ruby begin with ?
A. Lowercase
B. !
C. !
D. Uppercase
View Answer
Ans : D
Explanation: Constants begin with an uppercase letter.
8. Which of the following Ruby Pseudo-Variables is used for The receiver object of the current method?
A. self
B. current
C. nil
D. _FILE_
View Answer
Ans : A
Explanation: self : The receiver object of the current method
9. Which of the following Ruby Pseudo Variables is used to return the name of the current source file?
A. _FILE_
B. _CURRENT_
C. _LINE_
D. None of the above
View Answer
Ans : A
Explanation: __FILE__ : The name of the current source file.
10. Which of the following Ruby Pseudo Variables is used to return current line number in the source file?
A. _FILE_
B. _CURRENT_
C. _LINE_
D. None of the above
View Answer
Ans : C
Explanation: __LINE__ : The current line number in the source file.
11. Why
notation is used in Ruby?
A. Formfeed
B. Escape
C. Carriage return
D. Bell
View Answer
Ans : C
Explanation:
: Carriage return (0x0d)
12. Which notation is used for Octal notation?
A. \s
B. \xnn
C.
\n
D.
\nn
View Answer
Ans : D
Explanation:
nn : Octal notation (n being 0-7)
13. Which of the following datatypes are valid in Ruby?
A. Numbers
B. String
C. Boolean
D. All of the above
View Answer
Ans : D
Explanation: Ruby supports all the three (numbers,strings,boolean) data types.
14. Why can not we use quotation marks ("or") with boolean?
A. It indicates that we are talking about a string
B. It indicates that we are assining a value
C. It indicates that that we are replacing boolean data type with string data type
D. None of the above
View Answer
Ans : A
Explanation: Quotation marks are used only with strings.
15. What is the sequence of ruby strings?
A. 16-bit bytes
B. 8-bit bytes
C. 4-bit bytes
D. None of the above
View Answer
Ans : B
Explanation: They are simply the sequence of 8-bit bytes.
16. What is the output of the given code?
my_string=Ruby
puts(my_string)
A. Ruby
B. Nil
C. Error
D. my_string
View Answer
Ans : C
Explanation: Ruby should be written in double quotes inorder to indicate a string.
17. single-quoted strings don`t allow ?
A. substitution
B. backslash notation
C. single-quoted strings allow substitution and backslash notation.
D. single-quoted strings don`t allow substitution and allow backslash notation.
View Answer
Ans : D
Explanation: single-quoted strings don`t allow substitution and allow backslash notation only for \ and '.
18. Which type of number(0b1011 ) is ?
A. Octal
B. Hexadecimal
C. Binary
D. Decimal
View Answer
Ans : C
Explanation: Binary number is the answer.
19. Objects of which class does the integer from the range -2^30 to 2^(30-1) belong to?
A. Octal
B. Bignum
C. Fixnum
D. Binary
View Answer
Ans : C
Explanation: Integer within this range are objects of the class fixnum.
20. Objects of which class does the integer from the range -2^60 to 2^(60-1) belong to?
A. Octal
B. Bignum
C. Fixnum
D. Binary
View Answer
Ans : B
Explanation: Integer within this range are objects of the class Bignum..
Discussion