Q:

Ruby basics – Find output programs (set 1)

belongs to collection: Ruby Find Output Programs

0

Program 1:

def main
    print "Hello World";
    puts "Hello World";
end

Program 2:

def main
    int num = 20;
    string msg = "Hello World"
    
    puts "Num: ", num;
    puts "Message: ", msg;
end

main();

Program 3:

def main
    num = 20;
    msg = "Hello World"
    
    puts "Num: ", num;
    puts "Message: ", msg;
end

main();

Program 4:

def main
    num = 20;
    msg = "Hello World"
    
    print "Num: ", num;
    print "Message: ", msg;
end

main();

Program 5:

def main
    num = 20;
    msg = "Hello World"
    
    println "Num: ", num;
    println "Message: ", msg;
end

main();

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

Nothing will print.

Explanation:

In the above program, we created a method main(), but we did not call it. That is why nothing will be printed.

Answer Program 2:

Output:

main.rb:2:in `main': undefined method `int' for main:Object (NoMethodError)

Explanation:

The above program will generate a syntax error because "int" and "string" are not any predefined datatype in ruby.

 

Answer Program 3:

Output:

Num: 
20
Message: 
Hello World

Explanation:

In the above program, we created an integer variable num and a string variable msg. Then we printed the value of variables using the puts() method. The puts() method prints the newline by default.

Answer Program 4:

Output:

Num: 20Message: Hello World

Explanation:

In the above program, we created an integer variable num and a string variable msg. Then we printed the value of variables without newline character using the print() method.

Answer Program 5:

Output:

main.rb:5:in `main': undefined method `println' for main:Object (NoMethodError)

Explanation:

The above program will generate a syntax error because println() is not a predefined method in ruby.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby basics – Find output programs (set 2)... >>