Professional Documents
Culture Documents
Ruby Basic Exam Quessions
Ruby Basic Exam Quessions
boolean_1 = 77<78
puts(boolean_1)
a) Nil
b) True
c) False
d) Error
my_string=Ruby
puts(my_string)
a) Ruby
b) Nil
c) Error
d) None of the mentioned
a) 0-8
b) 0-7
c) 0-A
d) None of the mentioned
9. first_name,Last_name=gets.chomp,gets.chomp
is the correct way to get the input from the user.
a) True
b) False
10. What is the output of the given code?
city,state,pin=gets.chomp,gets.chomp,gets.
chomp
puts "Iam from #{city} city, #{state}
state, pincode: #{pin} "
a) Error in compilation
b) Give your address
c) What’s your address? Chennai
Tamilnadu
600048
Iam from Chennai city, Tamilnadu state, pincode:
600048
d) None of the mentioned
Ans=Ruby
puts "#{Ans} is an oop language"
puts "It is very efficient langauge"
puts "#{expr} is used on rails
platform"
a) Error, no output
b) Ruby is an oop language
It is very efficient langauge
undefined local variable
c) Ruby is an oop language
It is very efficient langauge
#{expr} is used on rails platform
d) None of the mentioned
Ans=Ruby
puts "#[Ans] is an oop language"
a) Error, no output
b) Ruby is an oop language
c) Warning:already initialized constant Ans
d) None of the mentioned
ruby.reverse
a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby
"Ruby".reverse.upcase
a) RUBY
b) ybuR
c) YBUR
d) YBUr
x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x +
y }."
puts "The average was #{ (x + y + z)/3
}."
a) 12,48,40
b) Syntax error
c) The value of x is 12.
The sum of x and y is 48.
The average was 40.
d) None of the mentioned
=begin
iam in a multi line comment
=end
a) True
b) False
= begin
# comment
= end
a) True
b) False
"Ruby Language".length
=begin
calculate length
=end
a) 13
b) 13 calculate length
c) 12
d) None of the mentioned
"Ruby Language".length
= begin
calculate length
= end
a) 13
b) 12
c) Error
d) None of the mentioned
if condition
code
end
a) True
b) False
2. If expression.
The expression can be of which type?
a) True
b) Any number
c) Any string
d) All of the mentioned
if 11<2
print "Eleven is less than two"
end
print "11 is greater"
a) 11 is greater
b) Eleven is less than two
c) Eleven is less than two 11 is greater
d) None of the mentioned
if 11>2
puts "Eleven is greater than two"
end
print "You'r right"
a) Eleven is greater than two
b) You’r right
c) Eleven is greater than two
You’r right
d) None of the mentioned
if 79>78
puts "True".upcase
if 9>8
puts "True".reverse
if 7==7
puts "equal".downcase
end
end
end
a) True
b) True eurt
c) TRUE
eurT
equal
d) equal
if 79>78
puts "True".upcase
if 9>8
puts "True".Upcase
if 7==7
puts "equal".downcase
end
end
end
a) True
b) Error
c) True,error
d) TRUE
Undefined method `Upcase’ for String
x=1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x!=0
puts "x is 1"
else
puts "I can't guess the number"
end
a) x is greater than 2
b) x is 1
c) I can’t guess the number
d) None of the mentioned
if 1>2
puts "false"
else
puts "True"
a) False
b) True
c) Syntax error
d) None of the mentioned
if 1>2
puts "false"
end
else
puts "True"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
variable=true
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
variable="true".reverse
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
variable=true
if !variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
variable="true".length
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) 4
counter=1
if counter<=5
puts (counter)
counter=counter+1
else
puts(counter)
counter=counter-1
end
a) 1, 2
b) 1,2,3,4,5
c) 1 2 1
d) 1
2
if !true
print "False"
elsif !true || true
print "True"
end
a) True
b) False
c) Syntax eroor
d) None of the mentioned
variable = false
if variable
print "false"
elsif !variable
print "true"
end
a) False
b) True
c) Syntax error
d) None of the mentioned
x=7
y=9
if x==y
print "equal"
elsif x>y
print "greater"
else
print "less"
end
a) equal
b) greater
c) less
d) none of the mentioned
a=true
b=false
if a && b
puts "False"
elsif a || b
puts "True"
else
puts "neither true nor false"
end
a) false
b) true
c) neither true nor false
d) none of the mentioned
9. What is the output of the given code?
a=true
b=false
if a && b
puts "False"
elsif a || b
puts "True"
else
puts "neither true nor false"
end
a) false
b) true
c) neither true nor false
d) none of the mentioned
x=3
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
end
a) x is greater than 2
b) x is less than 2
c) 3
d) None of the mentioned
var = 1
print "1 -- Value is set\n" if var
print "2 -- Value is set\n" unless var
var = false
print "3 -- Value is set\n" unless var
a) 1–Value is set
b) 2–Value is set
c) 1–Value is set
2–Value is set
d) 1–Value is set
3–Value is set
hungry=false
unless hungry
print "Not hungry"
else
print "Hungry"
end
a) Not hungry
b) Hungry
c) Syntax error
d) None of the mentioned
counter=12
unless counter
print counter+1
else
print counter+2
end
a) 13
b) 14
c) 15
d) None of the mentioned
x=8
y=10
unless x>y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end
a) x is less than y
b) x is less than y+1
c) x is less than y
x is less than y+1
d) None of the mentioned
x="ruby".length
y="language".length
puts x,y
unless x>y
print "length of x is less than that of y"
end
a)4
8
b)4
8
length of x is less than that of y
c) Syntax error
d) None of the mentioned
x=8
y=10
unless x<y
puts "x is less than y"
end
unless x>y+1
puts "x is less than y+1"
end
a) x is less than y
b) x is less than y+1
c) x is less than y
x is less than y+1
d) None of the mentioned
age = 5
case age
when 0 .. 2
puts "baby"
when 3 .. 6
puts "little child"
when 7 .. 12
puts "child"
when 13 .. 18
puts "youth"
else
puts "adult"
end
a) baby
b) adult
c) little child
d) youth
case expression
when expression , expression ... then
code ...
else
code
end
a) True
b) False
age = 4
case age
puts "baby" when 0 .. 2
puts "little child" when 3 .. 6
puts "child" when 7 .. 12
puts "youth" when 13 .. 18
puts "adult" else
end
a) adult
b) youth
c) child
d) syntax error
string = gets.chomp
case string
when string = "a"
print "alphabet a"
when string = "b"
print "alphabet b"
when string = "c"
print "alphabet c"
else
print "some other alphabet"
end
a) alphabet a
b) b
alphabet b
c) alphabet c
d) Syntax error
length=gets.chomp
case length.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error
length=gets.chomp
case length.reverse.length
when length=4
print "length is 4"
when length=5
print "length is 5"
end
a) ruby
length is 4
b) ruby
length is 5
c) abc
length is 4
d) syntax error
a=9!=10
a) True
b) False
c) Syntax error
d) None of the mentioned
test_1 = 17 > 16
puts(test_1)
test_2 = 21 <= 30
puts(test_2)
test_3 = 9 >= 9
puts(test_3)
test_4 = -11 > 4
puts(test_4)
a) True false true false
b) True True True False
c) False False True True
d) None of the mentioned
a="string"
b="strings"
if(a==b)
print ("a and b are same")
else
print "Not same"
end
a) a and b are same
b) Not same
c) a==b
d) None of the mentioned
6. Assignment operator is also known as relational
operator.
a) True
b) False
a=10
b=9
if(a>b)
print ("a greater than b")
else
print "Not greater"
end
a) a greater than b
b) Not greater
c) Syntax error
d) None of the mentioned
counter=1
if counter<=5
puts (counter)
counter=counter+1
a) Syntax error
b) 1 2
c) 1
d) None of the mentioned
Ruby Programming Questions and Answers –
Operators
1. Boolean opeartors are also known as logical
operators.
a) True
b) False
boolean_1 = !true
puts boolean_1
boolean_2 = !true && !true
puts boolean_2
a) True True
b) False False
c) True False
d) None of the mentioned
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
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
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
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
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
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
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
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
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
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
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
counter = true
while counter !=false
puts counter
end
a) True
b) False
c) Syntax error
d) Infinite loop
i = 0
while i < 5
puts i
i=(i+1)**2
end
a) 1 2 3 4 5
b) 0 1 4
c) 0 1
d) 1 4
a=5
b=15
while a&&b
puts a+b
end
a) 5..15
b) 20
c) Infinite loop
d) 5 15
a=5
b=15
while b>a
puts a*(b-a)
while a>b
a+=1
b-=1
end
end
a) 5..15
b) 50
c) Infinite loop
d) 5 50
i = 3
while i > 0 do
print i
i -= 1
end
a) 3
b) 321
c) Infinite loop
d) 3 2 1 0
i = 50
while i > 25 do
print 50/i
i -= 1
end
a) 50..25
b) 50..1
c) Infinite loop
d) 1111111111111111111111111
a = 5
b=10
while a<b do
puts a*b
a+=2
b-=2
end
a) 5 10
b) 50 56
c) Infinite loop
d) 5 6 7 8 9 10
i = 50
j=55
while i > 25 && j>35 do
puts 50*j/i
i -= 1
j-=2
end
a) 25 35
b) 50 55
c) Infinite loop
d) 55 54 53 52 51 50 48 47 46 45
i = 50
j=55
while i > 25 && i*j<100 do
puts (50*j)/i
i -= 1
j-=2
end
a) 25 35
b) No output
c) Infinite loop
d) 55 54 53 52 51 50 48 47 46 45
counter = 1
until counter > 10
puts counter
counter+=1
end
a) 1 2 3 4 5 6 7 8 9 10
b) 11 12 13 14 … infinite loop
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
counter = 0
until counter >= 10
puts counter
counter+=1
end
a) 1 2 3 4 5 6 7 8 9 10
b) 11 12 13 14 … infinite loop
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
i = 3
while i > 0 do
puts i
i -= 1
end
j = 3
until j == 0 do
puts j
j -= 1
end
a) 1 2 3 1 2 3
b) 3 2 1 3 2 1
c) 0 1 2 3 4 5 6 7 8 9
d) None of the mentioned
a="hungry"
until !a
puts "hungry"
a=!a
end
a) hungry
b) Nil
c) Error
d) None of the mentioned
m= 8
loop do
m += 2
puts m
break if m == 16
end
a) 10 12 14 16
b) Nil
c) Error
d) None of the mentioned
m=0
loop do
print "ruby"
m+=1
break if m==5
end
a) rubyrubyrubyrubyrubyruby
b) rubyrubyrubyrubyruby
c) Error
d) None of the mentioned
m=0
loop do
puts m*10
m+=1
break if m==5
end
a) 0 10 20 30 40
b) 10 20 30 40 50
c) Error
d) None of the mentioned
m=0
loop do
puts 101
m+=1
break if m==5
end
a) 101 101 101 101 101
b) 10 20 30 40 50
c) Error
d) None of the mentioned
m=5
loop do
m-=1
break if m==0
end
a) 1 2 3 4 5
b) 10 20 30 40 50
c) Error
d) Nil
Ruby Programming Questions and Answers –
For Loop
1. What is the output of the given code?
m= 0
loop do
m += 1
print m
break if m == 10
end
a) 12345678910
b) 1 2 3 4
c) 2 3 4 5
d) None of the mentioned
loop do
m += 1
puts m
break if m == 3
end
a) Garbage values
b) 1 1 2 3 4 1 2 3 9 1 2 3
c) 1 4 9 16 25
d) None of the mentioned
i=1
for i in 5..10
puts i^2
end
a) 7 4 5 10 11 8
b) 25 36 49 64 81 100
c) 1 4 9 16 25
d) None of the mentioned
i=5
j=10
for i in 5..10 && j in 5..10
puts i**j
end
a) Syntax error
b) 25 36 49 64 81 100
c) 1 4 9 16 25
d) None of the mentioned
my_array = [1, 2, 3, 4]
print my_array
a) [1, 2, 3, 4].
b) 1234
c) Error
d) None of the mentioned
array1 = [0,0,0]
array2 = [0,0,0]
if array1 == array2
print "They are equal"
else
print "Not equal"
end
a) They are equal
b) Not equal
c) Nil
d) None of the mentioned
9. What will be the output of the following?
array1 = [1,2,3]
array2 = [0,0,0]
if array1 >= array2
print "Greater or equal"
else
print "Not equal"
end
a) Greater or equal
b) Not equal
c) Error
d) None of the mentioned
array1 = [1,2,3]
array2 = [0,0,0]
if array1 == array2
print "Equal"
else
print "Not equal"
end
a) Equal
b) Not equal
c) Error
d) None of the mentioned
string_array = ["a","e","i","o","u"]
print string_array
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) Vowels
d) None of the mentioned
advertisement
string_array = ["a","e","i","o","u"]
print string_array[3]
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
d) None of the mentioned
string_array = ["a","e","i","o","u"]
boolean_array = ["True","False"]
puts string_array[3]
puts boolean_array
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
True
False
d) None of the mentioned
string_array = ["a","e","i","o","u"]
boolean_array = ["True","False"]
puts string_array[3]
puts boolean_array[1]
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
False
d) None of the mentioned
a=[1,2,3,4,5]
b=[1,2,4,6,8]
if a[3]==b[2]
print "Equal"
end
a) Equal
b) Error
c) 4
d) None of the mentioned
a=[1,2,3,4,5]
b=[1,2,3,4,5]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned
multi_d_array = [[0,0,0,0],[0,0,0,0],
[0,0,0,0],[0,0,0,0]]
print multi_d_array
a) [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]].
b) [0, 0, 0, 0].
c) [0, 0, 0, 0][0, 0, 0, 0].
d) None of the mentioned
multi_d_array = [[1,2,3,4],[0,0,0,0]]
multi_d_array.each { |x| puts "#{x}\n" }
a) [1, 2, 3, 4].
[0, 0, 0, 0].
[[1, 2, 3, 4], [0, 0, 0, 0]].
b) [0, 0, 0, 0].
c) [0, 0, 0, 0][0, 0, 0, 0].
d) None of the mentioned
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3],[0,0,0,0]]
if array1==array2
print "Equal"
else
print "Not equal"
end
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) Equal
c) Not equal
d) None of the mentioned
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3,4],[0,0,0,0]]
if array1==array2
print "Equal"
else
print "Not equal"
end
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) Equal
c) Not equal
d) None of the mentioned
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3,4],[0,0,0,0]]
print array1+array2
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]].
c) [[2,4,6,8],[0,0,0,0]].
d) None of the mentioned
7. What is the output of the given code?
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3,4],[0,0,0,0]]
print array1-array2
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]].
c) [].
d) None of the mentioned
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3,4],[0,0,0,0]]
print array1*array2
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]].
c) [].
d) Error
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3],[0,0,0]]
print array1 && array2
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]].
c) [].
d) Error
array1 = [[1,2,3,4,5],[0,0,0,0]]
array2 = [[1,2,3],[0,0,0]]
print array1 || array2
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4, 5], [0, 0, 0, 0]].
c) [].
d) Error
array1 = [[1,2,3,4,5],[0,0,0,0]]
array2 = [[1,2,3],[0,0,0]]
print !array1
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4, 5], [0, 0, 0, 0]].
c) False
d) Error
12. What is the output of the given code?
a=[["a","b"]]
b=[["e","a"]]
print a + b
a) [[“a”, “b”], [“e”, “a”]].
b) [[“2a”, “b”], [“e”]].
c) False
d) Error
string_array = ["a","e","i","o","u"]
print string_array
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) Vowels
d) None of the mentioned
string_array = ["a","e","i","o","u"]
print string_array[3]
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
d) None of the mentioned
string_array = ["a","e","i","o","u"]
boolean_array = ["True","False"]
puts string_array[3]
puts boolean_array
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
True
False
string_array = ["a","e","i","o","u"]
boolean_array = ["True","False"]
puts string_array[3]
puts boolean_array[1]
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) o
False
d) None of the mentioned
a=[1,2,3,4,5]
b=[1,2,4,6,8]
if a[3]==b[2]
print "Equal"
end
a) Equal
b) Error
c) 4
d) None of the mentioned
a=[1,2,3,4,5]
b=[1,2,3,4,5]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned