Ruby

All Questions for: Ruby

Question 1: What is the result of the expression: 5 / 2 in Ruby?

  • A 2.5
  • B 2
  • C 3
  • D 3.5

Explanation: The correct answer to the question is: 2

Question 2: Why is the library function 'puts'used for?

  • A Gets input from the user
  • B Prints whatever is given and print it to the screen
  • C None of the mentioned
  • D Prints whatever is given and insert a new(blank) line

Explanation: The correct answer to the question is: Prints whatever is given and insert a new(blank) line

Question 3: Why do we use comments?

  • A For later use to understand the working of the program
  • B It makes easy for the third person to understand
  • C All of the mentioned
  • D It is an efficient way of programming

Explanation: The correct answer to the question is: All of the mentioned

Question 4: Which method is used to convert a string to uppercase in Ruby?

  • A .to_upper
  • B .upcase
  • C .to_uppercase
  • D .uppercase

Explanation: The correct answer to the question is: .upcase

Question 5: What does the 'print' method do in Ruby?

  • A Reads input from user
  • B Prints to console
  • C Finds the length
  • D Converts to string

Explanation: The correct answer to the question is: Prints to console

Question 6: What is the output of the code?

  • A Amil Jones
  • B What's your first name? amil What's your last name? jones My name is Amil Jones
  • C None of the mentioned
  • D My name is Amil jones

Explanation: The correct answer to the question is: What's your first name? amil What's your last name? jones My name is Amil Jones

Question 7: What is the output of the given code?

  • A 10 20
  • B -5 -5 -5
  • C None of the mentioned
  • D 15 19 23

Explanation: The correct answer to the question is: -5 -5 -5

Question 8: What is the output of given code?

  • A I'am Learning RUBY Language
  • B Undefined method
  • C None of the mentioned
  • D "I'AM LEARNING RUBY LANGUAGE"

Explanation: The correct answer to the question is: "I'AM LEARNING RUBY LANGUAGE"

Question 9: Which of the following is NOT a valid data type in Ruby?

  • A Float
  • B Double
  • C Integer
  • D String

Explanation: The correct answer to the question is: Double

Question 10: In Ruby, what does the 'if' statement evaluate?

  • A String
  • B Boolean value
  • C Expression
  • D Integer

Explanation: The correct answer to the question is: Boolean value

Question 11: What will be the output of the given code?

  • A None of the mentioned
  • B True False
  • C True True
  • D False False

Explanation: The correct answer to the question is: True False

Question 12: Which of the following is a valid variable name in Ruby?

  • A _variable
  • B 123variable
  • C my_variable
  • D variable-1

Explanation: The correct answer to the question is: my_variable

Question 13: What is the output of the expression: 7 / 2 in Ruby?

  • A 4
  • B 3.5
  • C 3
  • D 2

Explanation: The correct answer to the question is: 3

Question 14: What is the correct way to cast a string to an integer in Ruby?

  • A string.convert(:integer)
  • B int(string)
  • C string.to_i
  • D string.integer

Explanation: The correct answer to the question is: string.to_i

Question 15: What will the following expression evaluate to? (true && false) | | (!true)

  • A Error
  • B None of the mentioned
  • C False
  • D True

Explanation: The correct answer to the question is: False

Question 16: Which of the following is the valid string method?

  • A The .downcase method
  • B The .upcase method
  • C The .irreverse method
  • D The .length method

Explanation: The correct answer to the question is: The .irreverse method

Question 17: The downcase method changes the whole string to smallcase letters.

  • A False
  • B True

Explanation: The correct answer to the question is: True

Question 18: Which method is used to convert a string to lowercase in Ruby?

  • A .downcase
  • B .lower
  • C .lowercase
  • D .to_lowercase

Explanation: The correct answer to the question is: .downcase

Question 19: What is the correct syntax for string interpolation in Ruby?

  • A "#{variable}"
  • B variable.to_string
  • C $(variable)
  • D %(variable)

Explanation: The correct answer to the question is: "#{variable}"

Question 20: What is the output of the expression: "5".to_i + 2 in Ruby?

  • A Error
  • B 7
  • C +7
  • D "52"

Explanation: The correct answer to the question is: 7

Question 21: What is the correct way to define a block in Ruby?

  • A block { code }
  • B block code
  • C function code
  • D do code end

Explanation: The correct answer to the question is: block { code }

Question 22: What is the result of the expression: 9 / 2 in Ruby?

  • A 2.5
  • B 2
  • C 4
  • D 4.5

Explanation: The correct answer to the question is: 2.5

Question 23: What will the following expression evaluate to? true | | false

  • A False
  • B Error
  • C True
  • D None of the mentioned

Explanation: The correct answer to the question is: True

Question 24: Which sequence can be used to substitute the value of any ruby expression in a string?

  • A None of the mentioned
  • B #expr
  • C #(expr)
  • D #{expr}

Explanation: The correct answer to the question is: #{expr}

Question 25: The following is the correct way to use multiline comment.

  • A True
  • B False

Explanation: The correct answer to the question is: False

Question 26: What is the output of the following?

  • A Undefined local variable ruby
  • B ybur
  • C Error in compilation
  • D 4

Explanation: The correct answer to the question is: Undefined local variable ruby

Question 27: What is the purpose of the require keyword in Ruby?

  • A Imports external libraries
  • B Defines a new module
  • C Creates a new class
  • D Loads a specific Ruby file

Explanation: The correct answer to the question is: Imports external libraries

Question 28: What is the correct way to define a symbol in Ruby?

  • A symbol = Symbol("value")
  • B symbol = 'value'
  • C symbol = "value"
  • D symbol = :value

Explanation: The correct answer to the question is: symbol = :value

Question 29: What is the correct syntax for accessing a class method?

  • A Class.new.method_name
  • B Class.method_name
  • C object.method_name
  • D method_name(Class)

Explanation: The correct answer to the question is: Class.method_name

Question 30: Why is gets not preferred instead of gets.chomp?

  • A All of the mentioned
  • B Gets remove an extra new line
  • C Gets add an extra new line while chomp removes it
  • D Chomp erases the value stored in the variable

Explanation: The correct answer to the question is: Gets add an extra new line while chomp removes it

Question 31: The '=' is used for assigning value to variable?

  • A False
  • B True

Explanation: The correct answer to the question is: True

Question 32: Which of the following is the correct way to define a variable in Ruby?

  • A var_name = value
  • B variable name = value
  • C var var_name = value
  • D name = value

Explanation: The correct answer to the question is: var_name = value

Question 33: What is the result of the expression: 3 ** 3 in Ruby?

  • A 27
  • B 9
  • C 3
  • D 6

Explanation: The correct answer to the question is: 27

Question 34: What is the default file extension for Ruby files?

  • A .ruby
  • B .rb
  • C .rby
  • D .ru

Explanation: The correct answer to the question is: .rb

Question 35: What do we mean by expression substitution?

  • A Substituting string functions
  • B Storing string value
  • C None of the mentioned
  • D Embedding the value of Ruby expression into a string using #{ }

Explanation: The correct answer to the question is: Embedding the value of Ruby expression into a string using #{ }

Question 36: What is the extension used for saving the ruby file?

  • A .ruby extension
  • B .rrb extension
  • C None of the mentioned
  • D .rb extension

Explanation: The correct answer to the question is: .rb extension

Question 37: What is the correct syntax to declare and initialize a variable in Ruby?

  • A variable var_name = value
  • B var_name = value
  • C var var_name = value
  • D variable_name = value

Explanation: The correct answer to the question is: variable_name = value

Question 38: Which of the following features does the 2.0 version of ruby supports?

  • A New literals
  • B Security fixes
  • C Method keyword arguments
  • D All of the mentioned

Explanation: The correct answer to the question is: All of the mentioned

Question 39: How do you check the truthiness of a value in Ruby?

  • A if value do code end
  • B if value != false
  • C if (value) then code end
  • D if value.is_truthy?

Explanation: The correct answer to the question is: if value != false

Question 40: How do you get the current date and time in Ruby?

  • A time = Time.now
  • B datetime = DateTime.now
  • C now = Date.today
  • D date = Date.now

Explanation: The correct answer to the question is: time = Time.now

Question 41: What is the correct syntax for a multiline comment in Ruby?

  • A # ... #
  • B // ... //
  • C ''' ... '''
  • D /* ... */

Explanation: The correct answer to the question is: ''' ... '''

Question 42: What does the && operator do in Ruby?

  • A Logical NOT
  • B Logical AND
  • C Bitwise AND
  • D Logical OR

Explanation: The correct answer to the question is: Logical AND

Question 43: What is the correct way to concatenate two strings in Ruby?

  • A str1 + str2
  • B str1.combine(str2)
  • C str1.concat(str2)
  • D str1.append(str2)

Explanation: The correct answer to the question is: str1 + str2

Question 44: In Ruby, what is the keyword used to define a constant?

  • A CONSTANT_NAME = value
  • B constantName(value)
  • C constant name : value
  • D constant_name = value

Explanation: The correct answer to the question is: CONSTANT_NAME = value

Question 45: Which operator is used for exponentiation in Ruby?

  • A //
  • B **
  • C ^^
  • D ^

Explanation: The correct answer to the question is: **

Question 46: What symbol is used to denote the end of a statement in Ruby?

  • A ,
  • B .
  • C :
  • D ;

Explanation: The correct answer to the question is: ;

Question 47: What is the size of an integer data type in ruby?

  • A -2 to 2
  • B -2 to 2
  • C -2 to 2
  • D -2 to 2

Explanation: The correct answer to the question is: -2 to 2

Question 48: What does the 'chomp' method do in Ruby?

  • A Converts to integer
  • B Splits a string
  • C Reads input from user
  • D Removes trailing whitespace

Explanation: The correct answer to the question is: Removes trailing whitespace

Question 49: What does the 'gets' method do in Ruby?

  • A Reads input from user
  • B Finds the length
  • C Prints to console
  • D Converts to string

Explanation: The correct answer to the question is: Reads input from user

Question 50: How do you call a method in Ruby?

  • A method_name(object)
  • B method_name()
  • C object.method_name()
  • D object.method_name

Explanation: The correct answer to the question is: object.method_name