JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD B.Tech IV SCRIPTING LANGUAGES Previous Question Paper (September - 2022 - R18)
Answer any five questions. All questions carry equal marks.
Main
Write and explain the lexical structure of ruby program.
Answer
JNTU
B.Tech ECE - R18
IV-II Semester - Scripting Languages - Exam Month-Year - Max Marks - Time
Instructions: Answer all questions.
The lexical structure of a programming language defines the set of valid characters, keywords, identifiers, operators, and delimiters that form the basic building blocks of a program. In Ruby, the lexical structure is relatively flexible and powerful, allowing for expressive code. It is primarily concerned with how source code is broken down into meaningful tokens.
Key components of Ruby's lexical structure include:
- Tokens: These are the smallest meaningful units in a program. Ruby's lexical analyzer (lexer) scans the source code and groups characters into tokens. Common token types are:
- Keywords: Reserved words with special meaning in Ruby (e.g.,
if,while,class,def,return,nil,true,false). These cannot be used as identifiers. - Identifiers: Names given to variables, methods, classes, modules, etc. They typically start with a lowercase letter or an underscore, followed by letters, numbers, or underscores. Constants start with an uppercase letter.
- Operators: Symbols that perform operations (e.g.,
+,-,*,/,=,==,!=,<,>,&&,||,!). - Literals: Direct representations of values. This includes numeric literals (integers, floats), string literals (e.g.,
"hello"), character literals (e.g.,?a), boolean literals (true,false), andnil. - Delimiters: Characters or sequences that separate or group other elements (e.g.,
(,),{,},[,],,,;,.,::). - Comments: Text ignored by the interpreter. In Ruby, comments start with a hash symbol (
#) and extend to the end of the line. Multi-line comments can be achieved using=beginand=endmarkers.
- Keywords: Reserved words with special meaning in Ruby (e.g.,
- Whitespace: Spaces, tabs, and newlines are generally ignored by the lexer, except when they are necessary to separate tokens (e.g., separating two keywords or an identifier and a keyword). Newlines often serve as statement terminators.
- Case Sensitivity: Ruby is a case-sensitive language.
myVariableis different frommyvariable. - Encoding: Ruby source files are typically expected to be in UTF-8 encoding.
Example Explanation:
Consider the following Ruby code snippet:
# This is a simple program
class Greeter
def initialize(name)
@name = name
end
def say_hello
puts "Hello, #{@name}!"
end
end
greeter = Greeter.new("World")
greeter.say_hello
When the Ruby interpreter processes this code, the lexer breaks it down into tokens:
# This is a simple program: A comment token.class: A keyword token.Greeter: An identifier token (specifically, a constant name).def: A keyword token.initialize: An identifier token (method name).(: A delimiter token.name: An identifier token (parameter name).): A delimiter token.@name: An identifier token (instance variable).=: An operator token."Hello, #{@name}!": A string literal token.puts: An identifier token (method name).;: A delimiter token (optional, as newline also acts as terminator).greeter: An identifier token (local variable).=: An operator token.Greeter: An identifier token (constant name)..: A delimiter token.new: An identifier token (method name).(,"World",): Delimiter and string literal tokens..: A delimiter token.say_hello: An identifier token (method name).(,): Delimiter tokens.
The structure ensures that the interpreter can correctly parse and execute the program by understanding these fundamental lexical elements.
How can we create an array using ruby? Explain with example program.
Answer
JNTU
B.Tech ECE - R18
IV-II Semester - Scripting Languages - Exam Month-Year - Max Marks - Time
In Ruby, arrays are ordered, mutable collections of objects. They are one of the most fundamental data structures. Ruby provides several flexible ways to create arrays.
1. Using Square Brackets `[]` (Literal Syntax):
This is the most common and idiomatic way to create an array. You enclose the elements within square brackets, separated by commas.
# Creating an array of integers
numbers = [1, 2, 3, 4, 5]
# Creating an array of strings
fruits = ["apple", "banana", "cherry"]
# Creating an array with mixed data types
mixed_array = [1, "hello", true, 3.14, nil]
# Creating an empty array
empty_array = []
puts numbers.inspect
puts fruits.inspect
puts mixed_array.inspect
puts empty_array.inspect
Output:
[1, 2, 3, 4, 5]
["apple", "banana", "cherry"]
[1, "hello", true, 3.14, nil]
[]
2. Using the `Array.new` Method:
The `Array.new` method provides more control, allowing you to specify the size of the array and optionally an initial value or a block to populate it.
- With size: Creates an array of a given size, filled with
nilby default. - With size and initial value: Creates an array of a given size, with each element initialized to the specified value.
- With a block: Creates an array where each element is the result of executing the block. The block can take an argument representing the index.
# Array with a specific size (filled with nil)
size_array = Array.new(5)
puts size_array.inspect # Output: [nil, nil, nil, nil, nil]
# Array with a specific size and initial value
filled_array = Array.new(3, "default")
puts filled_array.inspect # Output: ["default", "default", "default"]
# Array populated using a block (index-based)
indexed_array = Array.new(4) { |index| "item_#{index}" }
puts indexed_array.inspect # Output: ["item_0", "item_1", "item_2", "item_3"]
# Array populated using a block (calculating values)
calculated_array = Array.new(5) { |i| i * 2 }
puts calculated_array.inspect # Output: [0, 2, 4, 6, 8]
3. Using `%w` and `%W` (for String Arrays):
These are shortcuts for creating arrays of strings. `%w` creates an array of strings that are not subject to #{} interpolation and double quotes are not needed. `%W` is similar but allows interpolation and requires double quotes around elements containing spaces or special characters.
# Using %w (word array)
words = %w(this is a sample sentence)
puts words.inspect # Output: ["this", "is", "a", "sample", "sentence"]
# Using %W (interpolated word array)
name = "Ruby"
interpolated_words = %W(hello #{name} world)
puts interpolated_words.inspect # Output: ["hello", "Ruby", "world"]
These methods provide a comprehensive set of tools for creating arrays in Ruby, catering to various needs from simple lists to dynamically generated collections.
What is an event? Explain how can we bind the events.
Answer
Sign in to view the complete answer paper.
Write and explain the steps in writing CGI Script.
Answer
Sign in to view the complete answer paper.
How can we use the global variables in Ruby? Explain with an example program.
Answer
Sign in to view the complete answer paper.
How to embed Ruby in HTML? Explain with example program.
Answer
Sign in to view the complete answer paper.
Explain the procedure for embedding a ruby interpreter.
Answer
Sign in to view the complete answer paper.
Write a perl script to print the reverse order of lines in a file.
Answer
Sign in to view the complete answer paper.
Explain about the scalar data in perl.
Answer
Sign in to view the complete answer paper.
Describe in detail about the uses of scripting languages.
Answer
Sign in to view the complete answer paper.
With the help of a suitable example program, explain the working of patterns.
Answer
Sign in to view the complete answer paper.
How can we navigate between the files. Explain.
Answer
Sign in to view the complete answer paper.
Discuss about the role of references in perl programming.
Answer
Sign in to view the complete answer paper.
Explain the following advance TCL: eval, source, exec, uplevel.
Answer
Sign in to view the complete answer paper.