You are on page 1of 106

1.

Ruby, nesne yönelimli genel amaçlı bir programlama


dilidir.
a)Doğru
b) Yanlış

2. Aşağıdakilerden hangisi Ruby tarafından desteklenir?


a) Çoklu Programlama Paradigmaları
b) Dinamik Tip Sistemi
c) Otomatik Bellek Yönetimi
d) Hepsi

3. Ruby'nin 2.0 sürümü aşağıdaki özelliklerden hangisini


destekler?
a) Yöntem anahtar sözcüğü bağımsız değişkenleri
b) Yeni değişmezler
c) Güvenlik düzeltmeleri
d) Bahsi geçenlerin tümü

4. Aşağıdaki dil sözdiziminden hangisi Ruby'nin


sözdizimiyle eşleşir?
a) Perl
b) PHP
c) Java
d) Jquery

5. Ruby dosyasını kaydetmek için kullanılan uzantı nedir?


a) .ruby uzantısı
b) .rb uzantısı
c) .rrb uzantısı
d) Bahsi geçenlerin hiçbiri

7. Ruby, Köprü Metni Biçimlendirme Dili'ne (HTML)


gömülebilir.
a) Doğru
b) Yanlış
8. Ruby'nin bir derleyici kullanması şarttır.
a) Doğru
b) Yanlış
9. Ruby, internet ve intranet uygulamaları geliştirmek için
kullanılabilir.
a) Doğru
b) Yanlış
Ruby Sorular ve Cevaplar - Veri Türleri ve
Değişkenler

1. Ruby'de aşağıdaki veri türlerinden hangisi


geçerlidir?
a) Integer
b) Boolean değeri
c) Dize
d) Bahsi geçenlerin tümü

2. Herhangi bir değişken, Boole veri türündeyse ne


değerlendirecek?
a) Doğru
b) Sıfır
c) Yanlış
d) Doğru veya Yanlış

3. Neden boolean ile tırnak işaretleri (' veya “)


kullanamıyoruz?
a) Bir dizeden bahsettiğimizi gösterir
b) Bir değer atadığımızı gösterir
c) Boole veri türünü string veri türüyle
değiştirdiğimizi gösterir
d) Bahsi geçenlerin hiçbiri

4. Ruby büyük / küçük harfe duyarlı bir dildir.


a) Doğru
b) Yanlış

5. Ruby'de tamsayı veri türünün boyutu nedir?


a) -230 ila 230 arası
b) -230 ila 2 (30-1)
c)-229 için 230
d)-260 için 261

6. Ruby hem sayılarla hem de float değerleriyle


ilgilenebilir.
a) Doğru
b) Yanlış

7. Aşaiğdaki kodun çıktısı nedir?

boolean_1 = 77<78
puts(boolean_1)
a) Nil
b) True
c) False
d) Error

8. -2 ^ 30 ile 2 ^ (30-1) aralığındaki tamsayı hangi


sınıfın nesnelerine aittir?
a) Bignum
b) Octal
c) Fixnum
d) Binary
9. \b gösterimi ne anlama geliyor?
a) Escape
b) Space
c) Backspace
d) Newline

10. Ruby dizgi sıalamalaması nedir?


a) 16-bit bytes
b) 8-bit bytes
c) 10-bit bytes
d) None of the mentioned

11. Aşaiğdaki kodun çıktısı nedir?

my_string=Ruby
puts(my_string)
a) Ruby
b) Nil
c) Error
d) None of the mentioned

12. Aşağıdakilerden hangisi ruby'de geçerli bir veri


türü değildir?
a) Float
b) Integer
c) Binary
d) Timedate
13. Sekizli gösterim aralığı (\nnn) nedir?

a) 0-8
b) 0-7
c) 0-A
d) None of the mentioned

Ruby Questions and Answers – Library


Functions
1. Aşağıdakilerden hangisi geçerli bir ruby
kütüphanesinin fonksiyonu değildir?
a) Puts
b) Print
c) Gets
d) Get

2. Ruby'de 'puts' fonksiyonu ne için kullanılıyor?

a) Verilen her şeyi ekrana yazdırır


b) Verilen her şeyi yazdırır ve yeni (boş) bir satır
ekler
c) Kullanıcıdan girdi alır
d) Bahsi geçenlerin hiçbiri

3. Aşaiğdaki kodun çıktısı nedir?


print "Hey"
puts "Everyone!"
print "We are learning Ruby"
a) Error
b) Hey everyone we are learning Ruby
c) Hey everyone
d) Hey Everyone
We are learning Ruby

4. Ruby'de her print veya puts ifadesinden sonra


noktalı virgül veya parantez kullanırız.
a) Doğru
b) Yanlış

5. Ruby’de kullanıcıdan girdi almak için hangi


metot kullanılır?
a) get
b) gets.chomp
c) get-s
d) chomp

6. Aşaiğdaki kodun çıktısı nedir?

puts "what is your first name?"


name=gets.chomp
puts "what is your surname?"
surname=gets.chomp
a)Error in compilation
b) What is your first name?
xyz(enter any name)
What is your surname?
wtu(enter your surname)
c) Name=xyz
d) None of the mentioned

7. Which sequence can be used to substitute the


value of any ruby expression in a string?
a) #(expr)
b) #{expr}
c) #expr
d) None of the mentioned
8. Why is gets not preferred instead of
gets.chomp?
a) Gets add an extra new line while chomp
removes it
b) Gets remove an extra new line
c) Chomp erases the value stored in the variable
d) All 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?

print "What's your address"

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

11. What is the output of the the given code?

puts "My name is #{Name}"


a) Error in compilation
b) Name variable not defined earlier
c) My name is #{Name}
d) Undefined Error

12. What is the output of the given code?

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

13. What is the output of the given code?

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 Programming Questions and Answers –


String Methods
1. Which of the following is the valid string
method?
a) The .length method
b) The .upcase method
c) The .downcase method
d) The .irreverse method
2. The .length method is used to check number of
characters.
a) True
b) False

3. What is the output of the following?

"Iam learning ruby language".length


a) 26
b) 23
c) 20
d) 18

4. What is the output of the following?

ruby.reverse
a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby

5. What will be the output of the following?


"Eric".irreverse
a) Eric
b) cirE
c) undefined method
d) None of the mentioned

6. What does the .upcase method do?


a) Convert the string to uppercase
b) Convert only lowercase string to uppercase and
vice-versa
c) Convert the string to lowercase
d) None o the mentioned

7. What will be the output of the following?

"Ruby".reverse.upcase
a) RUBY
b) ybuR
c) YBUR
d) YBUr

8. What will be the output of the given code?

"I'am learning Ruby


Language".length.reverse.upcase
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum

9. What will be the output of the given code?

"I'am learning Ruby


Language".reverse.upcase.length
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum

10. The downcase method changes the whole


string to smallcase letters.
a) True
b) False

11. What will we the output of the given code?

"I'am Learning RUBY Language".downcase


a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) None of the mentioned

12. What is the output of given code?

string="I'am Learning RUBY


Language".downcase
string.upcase
a) Undefined method
b) “I’AM LEARNING RUBY LANGUAGE”
c) I’am Learning RUBY Language
d) None of the mentioned

13. What will be the output of the given code?

"Come let's learn.reverse Ruby.length


language".upcase
a) nreal s’tel emoC 4 LANGUAGE
b) Undefined error
c) “COME LET’S LEARN.REVERSE RUBY.LENGTH
LANGUAGE”
d) None of the above

14. What will we the output of the given code?

I'am Learning RUBY Language.downcase


a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) unterminated string meets end of file

15. Methods should not be written inside double


quotes.
a) True
b) False
Ruby Programming Questions and Answers –
Formatting String Methods
1. The .upcase and .capitalize method are used for
capitalizing the whole string.
a) True
b) False

2. What is the use of .capitalize method?


a) It capitalizes the entire string
b) It capitalize on the first letter of the string
c) It capitalize the strings which are in small case
d) All of the mentioned

3. What is the role of ! at the end of the capitalize


method?
a) It is the syntax for using capitalize method
b) It modifies the value stored in the variable
c) It indicates the termination of string
d) None of the mentioned

4. What is the output of the code?

print "What's your first name?"


first_name=gets.chomp
a=first_name.capitalize
first_name.capitalize!
print "What's your last name?"
last_name=gets.chomp
b=last_name.capitalize
last_name.capitalize!
puts "My name is #{first_name}
#{last_name}"
a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) My name is Amil Jones
d) None of the mentioned
5. What is the output of the code?
5. What is the output of the code?

print "What's your first name?"


first_name=gets.chomp
a=first_name.capitalize
first_name.capitalize!
print "What's your last name?"
last_name=gets.chomp
b=last_name.capitalize
last_name.capitalize
puts "My name is #{first_name}
#{last_name}"
a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) What’s your first name? amil
What’s your last name? jones
My name is Amil jones
d) None of the mentioned

6.What is the output of the given code?

print "what's your first name?"


first_name=gets.chomp
a=first_name.capitalize
"first_name.capitalize!".reverse
puts"My name is #{first_name}"
a) amil jones
b) What’s your first name? amil
My name is amil
c) My name is lima
d) My name is limA

7. What is the output of the given code?

print "what's your first name?"


first_name=gets.chomp
a=first_name.capitalize
"a".reverse
puts"My name is #{a}"
a) amil jones
b) What’s your first name? amil
My name is Amil
c) My name is lima
d) My name is limA

8. What is the output of the given code?

print "What's your first name?"


first_name=gets.chomp
a=first_name.capitalize
a=a.reverse
puts"My name is #{a}"
a) amil jones
b) What’s your first name? amil
My name is Amil
c) What’s your first name? amil
My name is limA
d) My name is limA

9. What do we mean by expression substitution?


a) Embedding the value of Ruby expression into a
string using #{ }
b) Substituting string functions
c) Storing string value
d) None of the mentioned

10. What is the output of the given code?

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

11. What does %{Learn ruby language}


represents?
a) “Learn Ruby language”
b) “%{Learn Ruby language}”
c) ” Learn Ruby language ”
d) None of the mentioned

12. What does %Q{ Learn ruby language }


represents?
a) ” Learn Ruby language ”
b) “%{ Learn Ruby language }”
c) ” Learn Ruby language ”
d) None of the mentioned

13. What does %x!ls! represents?


a) Same as back tick command output `ls`
b) Same as ‘x’
c) Same as “xls”
d) None of the mentioned

Ruby Programming Questions and Answers –


Comments in Ruby
1. Why do we use comments?
a) For later use to understand the working of the
program
b) It is an efficient way of programming
c) It makes easy for the third person to
understand
d) All of the mentioned

2. Which of the following type of comments are


valid in ruby?
a) Single line comments
b) Multi line comments
c) Double line comments
d) All of the mentioned

3. How to comment multiple lines in ruby?


a) Using multiline comments
b) Using single line comments
c) No option to comment
d) All of the mentioned
4. How to comment a single line?
a) Using #
b) Using begin and end
c) Using //
d) None of the mentioned

5. This the right way to comment a single line.


“Ruby”.length # I’m a single line comment!
a) True
b) False

6. What is the output of the given code?


“Ruby”.length #to find the length of given string
a) 4 to find the length of given string
b) 4
c) To find the length of given string
d) Ruby

7. The following syntax is used for multiline


comment.

=begin
iam in a multi line comment
=end
a) True
b) False

8. The following is the correct way to use multiline


comment.

= begin
# comment
= end
a) True
b) False

9. Why do we use =begin and =end?


a) To mark the start and end of multiline comment
b) To comment multiple lines
c) To avoid the use of # again and again
d) All of the mentioned

10. What is the output of the given code?

10. What is the output of the given code?

"Ruby Language".length
=begin
calculate length
=end
a) 13
b) 13 calculate length
c) 12
d) None of the mentioned

11. What is the output of the given code?

"Ruby Language".length
= begin
calculate length
= end
a) 13
b) 12
c) Error
d) None of the mentioned

12. Space between 2+5 or 2 + 5 is valid but =begin


and = begin is not valid.
a) True
b) False
Ruby Programming Questions and Answers –
The If Conditional Statement
1. The following syntax is correct for if conditional
statement.

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

3. What error does the if condition gives if not


terminated with end statement?
a) Syntax error
b) Unexpected end
c) Expecting keyword end
d) All of the mentioned

4. What is the output of the following?


if 1<2
print "one is less than two"
end
a) One is less than two
b) Syntax error
c) 1<2
d) None of the mentioned

5. What is the output of the code?

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

6. What is the output of the given code?

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

7. What is the output of the given code?

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

8. What is the output of the given code?

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

10. What is the output of the given code?

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

11. It’s a good habit to give two spaces between if


statement and condition.
a) True
b) False

Ruby Programming Questions and Answers –


The Else Conditional Statement

1. What is the use of else statement?


a) When the if condition is false then the next else
condition will get executed
b) When the if condition is false then the elsif
condition will get executed
c) When the if condition is false and if else
condition is true then only it will get executed
d) None of the mentioned

1. What is the use of else statement?


a) When the if condition is false then the next else
condition will get executed
b) When the if condition is false then the elsif
condition will get executed
c) When the if condition is false and if else
condition is true then only it will get executed
d) None of the mentioned,

3. What is the output of the given code?

if 1>2
puts "false"
else
puts "True"
a) False
b) True
c) Syntax error
d) None of the mentioned

4. What is the output of the code?

if 1>2
puts "false"
end
else
puts "True"
end
a) False
b) True
c) Syntax error
d) None of the mentioned

5. What is the output of the code?

variable=true
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned

6. What is the output of the code?

variable="true".reverse
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned

7. What is the output of the code?

variable=true
if !variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) None of the mentioned

8. What is the output of the code?

variable="true".length
if variable
puts "true"
else
puts "false"
end
a) False
b) True
c) Syntax error
d) 4

9. Which of the following is valid conditional


statement?
a) else
b) els
c) if else
d) None of the mentioned
10. What is the output of given code?

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

12. It is necessary that always if should come with


else block?
a) True
b) False
1. Which of the following is valid conditional
statement in Ruby?
a) elseif
b) elsif
c) else if
d) elseiff

2. The elsif conditional statement is written with


an expression.
a) True
b) False

3. The elsif statement can add many alternatives


to if/else statements.
a) True
b) False

4. What is the output of the given code?


counter=6
if counter<=5
puts (counter)
counter=counter+1
puts (counter)
elsif counter>5
puts (counter)
counter=2*counter
puts(counter)
else
puts(counter)
counter=counter-1
puts(counter)
end
a) 5
10
b) 6
12
c) Syntax error
d) None of the mentioned

5. What is output of the given code?


counter=-3
if counter<=5
puts (counter)
counter=counter+1
puts (counter)
elsif counter>5
puts (counter)
counter=2*counter
puts(counter)
end
a) -3
-2
b) -3
-6
c) Syntax error
d) None of the mentioned

6. What is the output of the given code?

if !true
print "False"
elsif !true || true
print "True"
end
a) True
b) False
c) Syntax eroor
d) None of the mentioned

7. What is the output of the given code?

variable = false
if variable
print "false"
elsif !variable
print "true"
end
a) False
b) True
c) Syntax error
d) None of the mentioned

8. What is the output of the given code?

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

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
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

The Unless Conditional Statement


1. Syntax for unless conditional statement is

unless conditional [then]


code
else
code
end
a) True
b) False

2. What is the output of the given code?

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

3. What is the output of the given code?

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

4. What is the output of the given code?

hungry=false
unless hungry
print "Not hungry"
else
print "Hungry"
end
a) Not hungry
b) Hungry
c) Syntax error
d) None of the mentioned

5. The following syntax is also used for unless


conditional statement.
code unless conditional
a) True
b) False

6. What is the output of the given code?

counter=12
unless counter
print counter+1
else
print counter+2
end
a) 13
b) 14
c) 15
d) None of the mentioned

7. What is the output of the given code?

unless true && false


print "false"
else
print "ruby"
end
a) True
b) False
c) Nil
d) Syntax error

8. What is the output of the given code?

print "2 is less than 3" unless 2>3


a) 2>3
b) 2 is less than 3
c) Syntax error
d) None of the mentioned

9. What is the output of the given code?

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

10. What is the output of the given code?

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

11. What is the output of the given code?

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

The Case Conditional Statement


1. What is the output of the given code?

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

2. The following syntax is used for the ruby case


statement.

case expression
when expression , expression ... then
code ...
else
code
end
a) True
b) False

3. What is the output of the given code?

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

4. What is the output of the given code?

for counter in 1..5


case counter
when 0 .. 2
puts counter
puts "baby"
when 3 .. 6
puts counter
puts "little child"
when 7 .. 12
puts counter
puts "child"
else
puts counter
puts "adult"
end
end
a) adult 1..5
b) 3..6 littlechild
c) 1
baby
2
baby
3
little child
4
little child
5
little child
d) adult

5. What is the output of the given code?

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

6. The expression specified by the when clause is


evaluated as the left operand. If no when clauses
match, case executes the code of the else clause.
a) True
b) False

7. What is the output of the given code?

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

8. What is the output of the given code?

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

9. What is the output of the given code?


l=9
case l
print "ruby" when l==9
print "language" when l==10
end
a) l==9
b) l==10
c) Syntax error
d) Ruby

10. A case statement compares the expression


specified by case and that specified by when using
the === operator and executes the code of the
when clause that matches.
a) True
b) False
Ruby Programming Questions and Answers –
Comparisons

1. The == ‘is equal to’ is known as relational


operator.
a) True
b) False

2. The ‘=’ is used for assigning value to variable?


a) True
b) False

3. What will the following code evaluate to?

a=9!=10
a) True
b) False
c) Syntax error
d) None of the mentioned

4. What is the output of the given code?

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

5. What is the output of the given code?

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

7. What is the output of the given code?

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

8. Which of the following are used for


comparison?
a) Equal to
b) Not equal to
c) Less than or greater than
d) All of the mentioned

9. What is the output of the given code?

if(a==10 && b=9)


print "true"
else
print "false"
end
a) True
b) False
c) Error
d) None of the mentioned

10. What is the output of the given code?

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

2. Which of the following is a valid boolean


operator?
a) and(&&)
b) or(||)
c) not(!)
d) All of the mentioned

3. The boolean operator && only result in true


when both the values are true?
a) True
b) False

4. What will the following expression evaluate to?


true && false
a) True
b) False
c) Error
d) None of the mentioned

5. What will be the output of the given code?

boolean_1 = 77 < 78 && 77 < 77


puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

6. What will the following expression evaluate to?


true || false
a) True
b) False
c) Error
d) None of the mentioned

7. What will the following expression evaluate to?


(true && false) || (!true)
a) True
b) False
c) Error
d) None of the mentioned

8. What will the following expression evaluate to?


!true && !false
a) True
b) False
c) Error
d) None of the mentioned

9. What will be the output of the given code?

boolean_1 = (3 < 4 || false) && (false ||


true)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

10. What will be the output of the given code?

boolean_1 = !(3 < 4 || false) && (false


|| true)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

11. What will be the output of the given code?

boolean_1 = 2**3 != 3**2 || true


puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

12. What will be the output of the given code?

boolean_1 = false || -10 > -9


puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

13. What will be the output of the given code?


boolean_1 = !(700 / 10 == 70)
puts boolean_1
a) True
b) False
c) Error
d) None of the mentioned

14. What will be the output of the given code?

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

15. What will be the output of the given code?

boolean_2 = !true && (!true || 100 !=


5**2)
puts boolean_2
a) True
b) False
c) Error
d) None of the mentioned

Ruby Programming Questions and Answers –


Assignment Operators

1. Which of the following is a valid assignment


operator?
a) +=
b) -=
c) *=
d) All of the mentioned

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

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

4. What is the output of the given code?

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

5. Ruby does not support ++ operator, it only


supports += operator.
a) True
b) False

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

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
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

9. The given two expression means the same.

counter=counter+1 and counter++


a) True
b) False

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

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

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

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
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

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

Ruby Programming Questions and Answers –


While Loop

1. While loop checks the condition and the loop


keeps on running till the condition is true, it stops
when the condition becomes false.
a) True
b) False

2. What is the output of the given code?


counter = 1
while counter < 11
puts counter
counter = counter + 1
end
a) Prints the number from 1 to 10
b) Prints the number from 1 to 11
c) Prints the number from 2 to 10
d) Infinite loop

3. What is the output of the given code?

counter = true
while counter !=false
puts counter
end
a) True
b) False
c) Syntax error
d) Infinite loop

4. What is the output of the given code?

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

5. What is the output of the given code?

a=5
b=15
while a&&b
puts a+b
end
a) 5..15
b) 20
c) Infinite loop
d) 5 15

6. What is the output of the given code?

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

7. What is the output of the given code?

i = 3
while i > 0 do
print i
i -= 1
end
a) 3
b) 321
c) Infinite loop
d) 3 2 1 0

8. What is the output of the given code?

i = 50
while i > 25 do
print 50/i
i -= 1
end
a) 50..25
b) 50..1
c) Infinite loop
d) 1111111111111111111111111

9. What is the output of the given code?

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

10. What is the output of the given code?

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

11. What is the output of the given code?

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

Ruby Programming Questions and Answers –


Until Loop
1. The complement of while loop is until loop.
a) True
b) False

2. What is the output of the given code?

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

3. What is the output of the given code?

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

4. What is the output of the given code?

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

5. What is the output of the given code?

a="hungry"
until !a
puts "hungry"
a=!a
end
a) hungry
b) Nil
c) Error
d) None of the mentioned

6. What is the output of the given code?

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

7. What is the output of the given code?

m=0
loop do
print "ruby"
m+=1
break if m==5
end
a) rubyrubyrubyrubyrubyruby
b) rubyrubyrubyrubyruby
c) Error
d) None of the mentioned

8. What is the output of the given code?

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

9. What is the output of the given code?

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

10. What is the output of the given code?

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?

for num in 1...5


puts num
end
a) 1 2 3 4 5
b) 1 2 3 4
c) 2 3 4 5
d) None of the mentioned

2. What does the 1…10 indicate?


a) Inclusive range
b) Exclusive range
c) Both inclusive and exclusive range
d) None of the mentioned

3. What is the output of the given code?

for num in 1..3


puts num
for i in 1..2
puts num*i
end
end
a) 1 2 3 4 5
b) 1 1 2 2 2 4 3 3 6
c) 2 3 4 5
d) None of the mentioned

4. 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

5. What is the output of the given code?

for num in 1..5


puts num*num
end
a) 12345678910
b) 1 2 3 4
c) 1 4 9 16 25
d) None of the mentioned

6. What is the output of the given code?

for num in 1..3


puts num*num
m= 0
loop do
m += 1
puts m
break if m == 3
end
end
a) 12345678910
b) 1 1 2 3 4 1 2 3 9 1 2 3
c) 1 4 9 16 25
d) None of the mentioned

7. What is the output of the given code?

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

8. What is the output of the given code?

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

9. What does the 1..10 indicate?


a) Inclusive range
b) Exclusive range
c) Both inclusive and exclusive range
d) None of the mentioned

10. What is the output of the given code?

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

Ruby Programming Questions and Answers –


Arrays
1. Arrays can be used to store multiple values in
one single variable.
a) True
b) False

2. What will be output of the given code?

my_array = [1, 2, 3, 4]
print my_array
a) [1, 2, 3, 4].
b) 1234
c) Error
d) None of the mentioned

3. Each element in an array has an index and the


starting index is index 1.
a) True
b) False

4. What will be the output of the following?

array = [100, 200, 300, 400, 500]


print array[4]
a) 400
b) 500
c) Nil
d) None of the mentioned

5. What will be the output of the following?

array = [100, 200, 300, 400, 500]


print array[5]
a) 400
b) 500
c) Nil
d) None of the mentioned

6. What will be the output of the following?

array = [100, 200, 300, 400, 500]


print "array[5]"
a) array[5].
b) 500
c) Nil
d) None of the mentioned

7. What will be the output of the following?


array1 = [100, 200, 300, 400, 500]
array2 = [1,2,3,4,5]
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

8. What will be the output of the following?

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

10. What will be the output of the following?

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

Ruby Programming Questions and Answers –


Arrays of Non-Numbers
1. It is possible to make array of booleans.
a) True
b) False

2. What is the output of the given code?

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

3. What is the output of the given code?

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

4. What is the output of the given code?

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

5. What is the output of the given code?

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

6. What is the output of the given code?

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

7. What is the output of the given code?

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

8. What is the output of the given code?

a=["hey", "ruby", "language"]


b=["hey", "ruby", "language"]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned

9. What is the output of the given code?

a=["hey", "ruby", "language"]


b=["hey", "language", "ruby"]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned

10. What is the output of the given code?

a=["hey", "ruby", "language"]


b=[1, 2, 3]
puts b[1]
puts a[2]
a) 3 ruby
b) Error
c) 2
language
d) None of the mentioned

Ruby Programming Questions and Answers –


Arrays of Arrays
1. Array of arrays are called multidimensional
arrays.
a) True
b) False

2. What is the output of the given code?

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

3. What is the output of the given code?

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

4. What is the output of the given code?

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

5. 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]]
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

6. 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) [[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

8. 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) Error

9. What is the output of the given code?

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

10. What is the output of the given code?

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

11. What is the output of the given code?

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

Ruby Programming Questions and Answers –


Introduction to Hashes
This set of Ruby Programming Interview
Questions and Answers for Experienced people
focuses on “Introduction to Hashes”.
1. It is possible to make array of booleans.
a) True
b) False

2. What is the output of the given code?

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

3. What is the output of the given code?

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

4. What is the output of the given code?

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

5. What is the output of the given code?

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

6. What is the output of the given code?

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

7. What is the output of the given code?

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

8. What is the output of the given code?


a=["hey", "ruby", "language"]
b=["hey", "ruby", "language"]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned

9. What is the output of the given code?

a=["hey", "ruby", "language"]


b=["hey", "language", "ruby"]
if a==b
print "Equal"
else
print "Not equal"
end
a) Equal
b) Error
c) Not equal
d) None of the mentioned
10. What is the output of the given code?

a=["hey", "ruby", "language"]


b=[1, 2, 3]
puts b[1]
puts a[2]
a) 3 ruby
b) Error
c) 2
language
d) None of the mentioned

You might also like