Data Types And Variables In Ruby MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Data Types And Variables In Ruby with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the expression: "5" + "2" in Ruby?
Correct Answer: Option C — 52
Detailed Academic Explanation:
Question: What is the output of the expression: "5" + "2" in Ruby? Explanation: In Ruby, when you use the "+" operator with strings (text enclosed in double quotes), it performs string concatenation , not mathematical addition. This means it joins the two strings together. So, "5" + "2" combines "5" and "2" to create the new string "52". Therefore, the correct answer is Option C: 52 Important Note: If you wanted to perform mathematical addition, you would need to use numbers without quotes, like 5 + 2, which would result in 7. But in this case, because the inputs are strings, Ruby treats the "+" as a string concatenation operator.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the data type of the value 3.14 in Ruby?
Correct Answer: Option B — Float
Detailed Academic Explanation:
Float is the correct answer to “What is the data type of the value 3.14 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the default value of a variable in Ruby if not initialized?
Correct Answer: Option B — nil
Detailed Academic Explanation:
nil is the correct answer to “What is the default value of a variable in Ruby if not initialized”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which symbol is used to declare a variable in Ruby?
Correct Answer: Option C — @
Detailed Academic Explanation:
@ is the correct answer to “Which symbol is used to declare a variable in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the 'nil' value represent in Ruby?
Correct Answer: Option B — Null value
Detailed Academic Explanation:
Null value is the correct answer to “What does the 'nil' value represent in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to convert a string to an integer in Ruby?
Correct Answer: Option A — .to_i
Detailed Academic Explanation:
.to_i is the correct answer to “What is the method used to convert a string to an integer in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to convert a string to a float in Ruby?
Correct Answer: Option A — .to_f
Detailed Academic Explanation:
.to_f is the correct answer to “What is the method used to convert a string to a float in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the data type of the value true in Ruby?
Correct Answer: Option D — Boolean
Detailed Academic Explanation:
Boolean is the correct answer to “What is the data type of the value true in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to convert a value to string in Ruby?
Correct Answer: Option A — .to_s
Detailed Academic Explanation:
.to_s is the correct answer to “What is the method used to convert a value to string in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the expression: 5 / 2 in Ruby?
Correct Answer: Option A — 2
Detailed Academic Explanation:
2 is the correct answer to “What is the result of the expression: 5 / 2 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is the correct way to declare a constant in Ruby?
Correct Answer: Option A — CONSTANT_NAME = value
Detailed Academic Explanation:
CONSTANT_NAME = value is the correct answer to “Which of the following is the correct way to declare a constant in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which method is used to check if a variable is nil in Ruby?
Correct Answer: Option B — .nil?
Detailed Academic Explanation:
.nil? is the correct answer to “Which method is used to check if a variable is nil in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check the data type of a variable in Ruby?
Correct Answer: Option C — .class
Detailed Academic Explanation:
.class is the correct answer to “What is the method used to check the data type of a variable in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check if a variable is defined in Ruby?
Correct Answer: Option B — .defined?
Detailed Academic Explanation:
.defined? is the correct answer to “What is the method used to check if a variable is defined in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the correct syntax to declare and initialize a variable in Ruby?
Correct Answer: Option D — variable_name = value
Detailed Academic Explanation:
variable_name = value is the correct answer to “What is the correct syntax to declare and initialize a variable in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is a valid way to declare a variable in Ruby?
Correct Answer: Option A — myVar = 10
Detailed Academic Explanation:
myVar = 10 is the correct answer to “Which of the following is a valid way to declare a variable in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the data type of the value "Hello, World" in Ruby?
Correct Answer: Option C — String
Detailed Academic Explanation:
String is the correct answer to “What is the data type of the value "Hello, World" in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check if a variable is empty in Ruby?
Correct Answer: Option A — .empty?
Detailed Academic Explanation:
.empty? is the correct answer to “What is the method used to check if a variable is empty in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the correct way to concatenate two strings in Ruby?
Correct Answer: Option B — str1 + str2
Detailed Academic Explanation:
str1 + str2 is the correct answer to “What is the correct way to concatenate two strings in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the expression: "5".to_i + 2 in Ruby?
Correct Answer: Option A — 7
Detailed Academic Explanation:
7 is the correct answer to “What is the output of the expression: "5".to_i + 2 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the correct way to declare a variable in Ruby without assigning a value?
Correct Answer: Option A — var_name
Detailed Academic Explanation:
var_name is the correct answer to “What is the correct way to declare a variable in Ruby without assigning a value”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to convert a string to lowercase in Ruby?
Correct Answer: Option A — .downcase
Detailed Academic Explanation:
.downcase is the correct answer to “What is the method used to convert a string to lowercase in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is the correct way to declare a variable in Ruby with a default value?
Correct Answer: Option A — var_name = value
Detailed Academic Explanation:
var_name = value is the correct answer to “Which of the following is the correct way to declare a variable in Ruby with a default value”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check if a variable is numeric in Ruby?
Correct Answer: Option B — .is_numeric?
Detailed Academic Explanation:
.is_numeric? is the correct answer to “What is the method used to check if a variable is numeric in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the expression: "10" * 3 in Ruby?
Correct Answer: Option A — "101010"
Detailed Academic Explanation:
"101010" is the correct answer to “What is the output of the expression: "10" * 3 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which method is used to convert a string to an array of characters in Ruby?
Correct Answer: Option D — .chars
Detailed Academic Explanation:
.chars is the correct answer to “Which method is used to convert a string to an array of characters in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check if a variable is of a specific class in Ruby?
Correct Answer: Option A — .is_a?
Detailed Academic Explanation:
.is_a? is the correct answer to “What is the method used to check if a variable is of a specific class in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is the correct way to define a variable in Ruby?
Correct Answer: Option A — var_name = value
Detailed Academic Explanation:
var_name = value is the correct answer to “Which of the following is the correct way to define a variable in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the method used to check if a string contains a specific substring in Ruby?
Correct Answer: Option C — .include?
Detailed Academic Explanation:
.include? is the correct answer to “What is the method used to check if a string contains a specific substring in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of the expression: 10 % 3 in Ruby?
Correct Answer: Option A — 1
Detailed Academic Explanation:
1 is the correct answer to “What is the result of the expression: 10 % 3 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Ruby, what is the keyword used to define a constant?
Correct Answer: Option C — constantName(value)
Detailed Academic Explanation:
constantName(value) is the correct answer to “In Ruby, what is the keyword used to define a constant”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is the correct way to define a range in Ruby?
Correct Answer: Option A — (1..10)
Detailed Academic Explanation:
(1..10) is the correct answer to “Which of the following is the correct way to define a range in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the 'return' keyword do in Ruby methods?
Correct Answer: Option A — Exits the method with a value
Detailed Academic Explanation:
Exits the method with a value is the correct answer to “What does the 'return' keyword do in Ruby methods”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the correct way to declare and initialize an array in Ruby?
Correct Answer: Option A — array_name = []
Detailed Academic Explanation:
array_name = [] is the correct answer to “What is the correct way to declare and initialize an array in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Ruby, what is the default file extension for Ruby files?
Correct Answer: Option C — .rb
Detailed Academic Explanation:
.rb is the correct answer to “In Ruby, what is the default file extension for Ruby files”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the 'chomp' method do in Ruby?
Correct Answer: Option A — Removes trailing whitespace
Detailed Academic Explanation:
Removes trailing whitespace is the correct answer to “What does the 'chomp' method do in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the correct syntax for a multiline comment in Ruby?
Correct Answer: Option C — ''' ... '''
Detailed Academic Explanation:
''' ... ''' is the correct answer to “What is the correct syntax for a multiline comment in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which method is used to find the length of an array in Ruby?
Correct Answer: Option B — .length
Detailed Academic Explanation:
.length is the correct answer to “Which method is used to find the length of an array in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the expression: 7 / 2 in Ruby?
Correct Answer: Option A — 3
Detailed Academic Explanation:
3 is the correct answer to “What is the output of the expression: 7 / 2 in Ruby”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Ruby, what is the keyword used to define a loop that repeats until a condition is true?
Correct Answer: Option A — until
Detailed Academic Explanation:
until is the correct answer to “In Ruby, what is the keyword used to define a loop that repeats until a condition is true”. This question is standard in competitive examination syllabi for Data Types And Variables In Ruby.
💡Key Conceptual Anchor:Data Types And Variables In Ruby
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.