CS508 – Modern Programming Languages: Midterm Important Subjective (95 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS508 Course Page · CS508 MCQ Pack (222 MCQs)
-
Write a note on ALGOL 58 and ALGOL 60, including their salient features.
Topic: Language History & Evolution
ALGOL stands for Algorithmic Language. ALGOL 58 was designed in 1958 in the search for a "Universal Language". Salient features of the language are: • Concept of type was formalized • Names could have any length • Arrays could have any number of subscripts • Lower bound of an array could be defined • Parameters were separated by mode (in & out) • Subscripts were placed in brackets • Compound statements were introduced (begin ... end) • Semicolon as a statement separator was used • Assignment operator was := • if had an else-if clause ALGOL 60 (1960) was a modified version of ALGOL 58. It added new features to the language, including block structure (local scope), two parameter passing methods – value and name, subprogram recursion, and stack-dynamic arrays – run time size definition and space allocation. It did not have built-in I/O facility.
-
Write a note on COBOL (1960): its design goals and the features it introduced.
Topic: Language History & Evolution
COBOL was designed in 1960 to support business-oriented computation which requires fixed point arithmetic. It was designed to look like simple English to broaden the base of computer users. It was thus required to be easy to use, even if that means it will be less powerful. Another design consideration was that it must not be biased by current compilers. It encountered problems because the design committee members were all from computer manufacturers and DoD branches and there were fights among manufacturers. It was the first language to add macro facility in a high-level language. It also introduced hierarchical data structures (records) and nested selection statements. The language supported long variable names (up to 30 characters) with hyphens, data division, and fixed-point arithmetic. It was the first language required by DoD and would probably have failed without support from DoD. It is still a very widely used business applications language and is very popular in business and government, much less at universities.
-
What are the additional features added in COBOL that were not included in SNOBOL?
Topic: Language History & Evolution • 2 marks marks
1. COBOL has introduced hierarchical data structures (records) and nested selection statements. 2. The COBOL language supported long variable names (up to 30 characters) with hyphens, data division, and fixed-point arithmetic. 3. These are the features in COBOL that were added or enhanced and were not included in SNOBOL.
-
Which language had a strong influence on high-level system programming languages like C?
Topic: Language History & Evolution
ALGOL 68 came from the continued development of ALGOL 60, but it is not a superset of that language. Its design is based on the concept of orthogonality. ALGOL 68 had even less usage than ALGOL 60 but had strong influence on subsequent languages, especially C, Ada and Pascal.
-
Which of the languages first provided a structured exception handling mechanism among Ada and C++?
Topic: Language History & Evolution
PL/1 introduced the concept of exception handling, but Ada elaborated this concept. • Ada was the first language to provide a structured exception handling mechanism. • Ada includes an exception handling mechanism so that these problems can be dealt with at run time. • Errors can be signaled as exceptions and handled explicitly.
-
Write three descendants of the C language.
Topic: Language History & Evolution
• C++: C++ is an extension of C, developed by Bjarne Stroustrup in 1983. C++ introduced the concept of classes and objects, which allows for the creation of user-defined data types and the use of object-oriented programming techniques. It also includes features such as function overloading, templates, and exception handling. • C#: C# is a modern, object-oriented programming language developed by Microsoft in 2000. It is designed for the Microsoft .NET framework and is commonly used for developing Windows desktop and web applications. C# is similar to C++ and Java in terms of syntax and features, but it also includes features such as garbage collection and built-in support for threading and parallel programming. • Objective-C: Objective-C is an object-oriented programming language that is a descendant of C. It was developed in the early 1980s by Brad Cox and Tom Love. Objective-C is widely used on Apple's macOS and iOS operating systems, as well as their respective APIs, Cocoa and Cocoa Touch. Objective-C is known for its dynamic binding, which allows for the creation of runtime-modifiable programs. These are just a few examples of the many descendants of the C language; there are many more in the market like Go, Rust, Swift, etc.
-
Write a simple "Hello World" program in Pascal, with starting and ending statements.
Topic: Language History & Evolution
program HelloWorld; begin writeln('Hello World'); end.
-
How can we increase readability? Write at least 5 points.
Topic: Language Evaluation Criteria & Design Issues • 3 marks marks
• Simplifying sentence structure: Using shorter, simpler sentences can improve readability for readers of all language backgrounds. • Using active voice: Writing in active voice (as opposed to passive voice) can make sentences clearer and more direct. • Using common words: Avoiding technical jargon or uncommon words can make text more accessible to a wider audience. • Using headings and subheadings: Breaking text up into smaller sections with headings and subheadings can make it easier to skim and understand. • Using images and other media: Adding images, videos, or other media can help to clarify or illustrate information, making it easier to understand.
-
Explain simplicity and readability as characteristics of a programming language.
Topic: Language Evaluation Criteria & Design Issues
• Simplicity refers to how easy it is to understand and use a language. A simple language is one that has a small set of features, is easy to learn, and has a straightforward syntax. Simple languages are often preferred for small projects, learning to code, or for people who are not professional programmers. • Readability refers to how easy it is to understand the code written in a particular language. A readable language is one that follows a consistent style, uses meaningful variable and function names, and has clear and concise code. Readable code is often preferred by professional programmers as it makes it easier to maintain and debug the code.
-
Considering 'Generality' as an important factor that helps to manage complexity of any programming language, briefly mention how generality can be considered an important language evaluation criterion.
Topic: Language Evaluation Criteria & Design Issues
Generality is an important factor that helps to manage complexity of any programming language and deals with the applicability of the language to a range of different domains. For example: C is more general purpose than FORTRAN or LISP, so it can be used in more domains than these two languages.
-
Explain the concept of dangling else with an example, and also explain the solution to this problem.
Topic: Language Evaluation Criteria & Design Issues
"Dangling else" in C explains the association of a single else statement in a nested if statement. In nested if statements, when a single "else clause" occurs, the situation that happens is called dangling else. For example: if (condition) if (condition) if (condition) else printf("dangling else!\n"); /* dangling else, as to which if statement does the else clause associate? */ Solution to the problem: In such situations, the else clause belongs to the closest if-statement which is incomplete, that is the innermost if-statement. We can make the else clause belong to the wanted if statement by enclosing the if statements in a block { } to which the else clause should associate. For example: if (condition) { if (condition) if (condition) } else printf("else associates with the outermost if statement!\n");
-
What does LISP stand for? Who designed it, and when?
Topic: LISP
LISP, an acronym for LIST Processing, is a programming language that was designed for easy manipulation of data strings. Developed in 1959 by John McCarthy as part of AI research at MIT, it is a commonly used language for artificial intelligence (AI) programming. It is one of the oldest programming languages still in relatively wide use. In LISP, all computation is expressed as a function of at least one object. Objects can be other functions, data items (such as constants or variables), or data structures. LISP's ability to compute with symbolic expressions rather than numbers makes it convenient for AI applications.
-
Write two points that differentiate the LISP language from other languages.
Topic: LISP • 2 marks marks
1. A LISP program has two types of elements: • Atoms • Lists 2. The first symbol is the name of the function. The rest are its arguments. For example: > (+ 1 3 5) 9
-
Which data structure is more widely used in LISP programming?
Topic: LISP
Atoms and Lists.
-
Set the value of variable a: HEIGHT = 12 and Weight = 5 (using property lists in LISP).
Topic: LISP • 3 marks marks
> (setf (get 'a 'height) 12) > (setf (get 'a 'weight) 5)
-
What is the difference between the statements > (set-intersection L1 L2) and > (set-difference L1 L2)? Also describe the related set operations.
Topic: LISP • 5 marks marks
These are set operations in LISP: > (union L1 L2) ; returns the union of the two lists > (intersection L1 L2) ; returns the intersection of the two lists > (set-difference L1 L2) ; returns the difference of the two lists (elements of L1 that are not in L2)
-
Write a function in LISP that returns the difference of the two lists L3 and L4.
Topic: LISP
These are set operations in LISP: > (union L1 L2) ; returns the union of the two lists > (intersection L1 L2) ; returns the intersection of the two lists > (set-difference L1 L2) ; returns the difference of the two lists So the difference of L3 and L4 is obtained by (set-difference L3 L4).
-
Which predicate function will help for testing atoms in LISP?
Topic: LISP
The predicate for testing whether an object is an atom is atom. (atom x) returns T if x is an atom and NIL if x is a list. Note: the source file's given answer, (member 'b L), tests whether an element is a member of a list — it does not test for atoms.
-
What is the error in > (sqrt x) in LISP?
Topic: LISP • 3 marks marks
x is unbound, because this symbol doesn't have any value. We will have to assign a value using setq, setf and set. > (setq x 3) 3
-
How many elements are there in (a (d e)) in LISP?
Topic: LISP • 2 marks marks
There are two elements: 1. Atom = a 2. List = (d e)
-
Enlist any three major concepts that were introduced for the first time in the LISP programming language.
Topic: LISP • 3 marks marks
From a programming language design point of view, LISP was the first to introduce the following concepts: • Conditionals: such as the if-then-else construct. • Function types: where functions are just like integers and strings. • Recursion: first language to support it. • Dynamic typing: all variables are pointers. • Garbage Collection. • Programs composed of expressions. • A symbol type. • The whole program is a mathematical function.
-
Which programming language is considered to be the de facto standard for the Artificial Intelligence community?
Topic: LISP • 2 marks marks
LISP — it was one of the earliest programming languages and is the standard language of the AI community.
-
Name the two famous versions (dialects) of LISP programming.
Topic: LISP
1) Scheme 2) Common Lisp
-
Differentiate the car, cdr, first and rest LISP functions.
Topic: LISP
first (or car) takes a list as an argument and returns the first element of that list. For example: > (first '(a s d f)) a rest (or cdr) takes a list as its argument and returns a new list after removing the first element from the list. For example: > (rest '(a s d f)) (s d f)
-
Identify the list selector functions used in LISP.
Topic: LISP
List Selectors: In order to select elements from a list, selector functions are used. There are two basic selector functions known as first (or car) and rest (or cdr). 1. First (or car) is a list selector. It takes a list as argument and returns the first element of the list. 2. Rest (or cdr) is a list selector. It takes a list as argument and returns the whole list after excluding the first element.
-
Write three built-in functions used in LISP.
Topic: LISP
• car: The car function returns the first element of a list. For example, (car '(1 2 3)) would return 1. • cdr: The cdr function returns the rest of the elements in a list after the first element. For example, (cdr '(1 2 3)) would return the list (2 3). • cons: The cons function is used to construct a new list by adding an element to the front of an existing list. For example, (cons 1 '(2 3)) would return the list (1 2 3). These are just a few examples of the many built-in functions in Lisp; there are many more like eq, equal, atom, list, +, -, *, /, etc.
-
Enlist any three mathematical functions used in LISP.
Topic: LISP • 3 marks marks
There are many built-in functions in LISP. These include math functions as well as functions for manipulating lists. The math functions include: +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min
-
Why is a predicate called a special function in LISP?
Topic: LISP
A predicate is a special function which returns NIL if the predicate is false, and T or anything other than NIL otherwise. Predicates are used to build Boolean expressions in the logical statements. Comparative operators in LISP are used as functions for numerical values and return a T or NIL: =, >, <, >=, <=. For example 1: (= (+ 2 4) (* 2 3)) T Example 2: (> (- 5 2) (+ 3 1)) NIL
-
Write a procedure in LISP that takes two linked lists as arguments and returns the concatenation of these two lists by adding one list in front of another. List a = (3 4 5), List b = (C d 6).
Topic: LISP • 5 marks marks
append takes two lists as arguments and appends one list in front of the other, as shown below: > (append '(a b) '(c d)) (A B C D) ; appends one list in front of another
-
Suppose you want to remove a certain property attribute named 'WEIGHT' from the following list of symbols in LISP: > (symbol-plist 'a) -> (WEIGHT 20 HEIGHT 8). How will you do it?
Topic: LISP
We can remove a property by using the remprop function as shown below: > (remprop 'a 'WEIGHT) T > (symbol-plist 'a) (HEIGHT 8) > (remprop 'a 'HEIGHT) T > (symbol-plist 'a) NIL
-
Explain the dotimes and dolist looping statements in LISP.
Topic: LISP • 3 marks marks
In LISP there are two basic statements for looping: dotimes and dolist. dotimes is like a counter-controlled for loop. Its syntax is given below: (dotimes (count n result) body) It executes the body of the loop n times, where count starts with 0 and ends with n-1. dolist: The second looping structure is dolist. It is used to iterate over the list elements, one at a time. Its syntax is given below: (dolist (x L result) body)
-
Explain the conditional control statements of LISP (if, when, cond).
Topic: LISP • 5 marks marks
LISP has multiple conditional control statements. The set includes if, when, and cond. If statement: The if statement has the following syntax: (if <test> <then> <else>) That is, the if statement has three parts: the test, the then part, and the else part. It works almost exactly like the if statement in C++. If the test is TRUE then the then part will be executed, otherwise the else part will be executed. If there is no else part, then if the test is not true the if statement will simply return NIL. Here is an example that shows the if statement: > (setq SCORE 78) 78 > (if (> score 85) 'HIGH (if (and (< score 84) (> score 65)) 'MEDIUM 'LOW)) MEDIUM In the above if statement, the then part contains 'HIGH and the else part is another if statement. So, with the help of nested if statements we can develop code with multiple branches.
-
Lists are used in LISP. Write the top elements of the lists given as (add 3 (mult 3 4)) and (job car school).
Topic: LISP • 3 marks marks
The first-level elements in LISP are called top-level elements. For example, the top elements of list (a b c) are a, b, and c. Similarly, the top elements of list (a (b c)) are a and (b c). An empty list is represented by NIL. It is the same as ( ).
-
Tell the output of the following LISP code: (defun y-min (x) (- x y)); set y to 2; then call (y-min 20).
Topic: LISP
-18
-
Explain briefly the expression of the LISP language (S-expression).
Topic: LISP
S-expression: An S-expression (S stands for symbolic) is a convention for representing data or an expression in a LISP program in text form. It is characterized by the extensive use of prefix notation with explicit use of brackets (affectionately known as Cambridge Polish notation). S-expressions are used for both code and data in Lisp. S-expressions were originally intended only as machine representations of human-readable representations of symbols, but Lisp programmers soon started using S-expressions as the default notation. S-expressions can either be single objects or atoms such as numbers, or lists.
-
What is the role of an atom in LISP?
Topic: LISP
Atoms: Atoms include numbers, symbols, and strings. LISP supports both real numbers and integers.
-
Identify the local and global variables in: > (defun y-plus (x) (+ x y))
Topic: LISP
This function adds x to y, where x is the parameter passed to the function and y is a global variable since it is not defined inside the function. It works in the following manner: > (setq y 2) > (y-plus 23) 25 With this we introduce the concept of local and global variables. Local variables are defined in the function body. Everything else is global.
-
Describe the following SNOBOL statement fields: (label), :S(label), :F(label), and :S(label2) F(label3).
Topic: SNOBOL • 2 marks marks
(label) This is a command for unconditional jump to the label specified inside parentheses. :S(label) This is a command for jump. It is used to jump to the specified label if the statement in the body was executed successfully or resulted in a true Boolean value. :F(label) It is opposite to :S(label). The control jumps to the label if the statement is not executed successfully or resulted in false. :S(label1) F(label2) It is a combination of :S(label) and :F(label). It states that go to label1 in case of success and label2 in case of failure.
-
What is the functionality of KEYWORD = 'INT' | 'CHAR' in SNOBOL4?
Topic: SNOBOL • 2 marks marks
KEYWORD = 'INT' 'CHAR' This statement assigns the pattern 'INT' 'CHAR' to the variable KEYWORD.
-
Pattern matching is a powerful tool for syntax analysis in SNOBOL. Consider the following piece of code and identify if the pattern matching comes out to be success or failure. Justify your answer in either case. FIRST = 'SUMMER'; SECOND = 'SUM'; FIRST SECOND
Topic: SNOBOL
If 'SUM' will be searched in 'SUMMER'. Since it is present in it — SUM ('MER') — it will result in success.
-
Explain pattern matching and string concatenation (the space operator) in SNOBOL.
Topic: SNOBOL
In SNOBOL, space is used as the pattern matching operator and the statement has the following form: subject pattern There is no assignment operator in this case. Both the subject and the pattern are strings, and this statement tries to match the pattern in the subject. It will be successful if the match is found and will result in failure otherwise. Pattern matching and manipulation is a very important feature of SNOBOL. Example: TRADE = 'PROGRAMMER' PART = 'GRAM' TRADE PART
-
Write down the working and output of the string replacement function in SNOBOL with respect to the following code: TEXT = "A {I,J} = A{A,B}+5"; OUTPUT = REPLACEMENT(TEXT, '{}', '[]')
Topic: SNOBOL • 5 marks marks
The replacement statement is used in conjunction with the pattern matching statement in the following manner: subject pattern = object In this case the pattern is searched in the subject and if found it is replaced by the object, as demonstrated by the following example: SENTENCE = 'THIS IS YOUR PEN' SENTENCE 'YOUR' = 'MY' Since 'YOUR' is present in the subject, it will be replaced by 'MY', resulting in changing the value of SENTENCE to 'THIS IS MY PEN'. If we now have the following statement: SENTENCE 'MY' = then SENTENCE will be further modified to 'THIS IS PEN', as we are now replacing 'MY' with a NULL string, effectively deleting 'MY' from the subject.
-
While programming in SNOBOL4, which of the following operators must not have space between operator and operand: (a) Unary operators (b) Binary operators?
Topic: SNOBOL • 2 marks marks
Unary operators.
-
How many types of functions are supported in SNOBOL4?
Topic: SNOBOL
SNOBOL4 supports two types of functions: 1. Built-in functions, which are known as primitive functions. 2. User defined functions.
-
How many types of statements are used for pattern building in SNOBOL?
Topic: SNOBOL • 2 marks marks
There are two types of statements for pattern building. These are Alternation and Concatenation.
-
Describe arrays in SNOBOL.
Topic: SNOBOL
In SNOBOL, arrays are created with the built-in ARRAY function. The prototype string given to ARRAY specifies the dimensions and the bounds of the subscripts, e.g. A = ARRAY('10') creates a one-dimensional array of 10 elements with subscripts 1 to 10, and ARRAY('2,8') creates a two-dimensional array. Array elements are accessed with angle-bracket notation, e.g. A<3> or N<I,J>. All elements are initially set to the null (empty) string unless an initial value is supplied as the second argument of ARRAY. Arrays in SNOBOL can hold values of mixed types, and subscripts can also be given as lower:upper bound pairs, e.g. ARRAY('-5:5'). (The source file gives no answer for this question — it only refers to a handout page.)
-
Write a two-dimensional array of size (2x8) in SNOBOL and initialize it to zero.
Topic: SNOBOL • 3 marks marks
The following statement creates a two-dimensional array of 2 x 8 and each cell is initialized to the NULL string: N = ARRAY('2,8')
-
Following is the snippet from SNOBOL4. Calculate the value of x, y, z where ** sign is used for exponentiation: 1) x = 2*3*2 2) y = 3*2*3 3) z = x + y**2
Topic: SNOBOL
In SNOBOL4, the ** operator is used for exponentiation. So, with that in mind: • x = 2*3*2 = 12 • y = 3*2*3 = 18 • z = x + y**2 = 12 + 18**2 = 12 + 324 = 336 So, the final values of x, y, and z are 12, 18 and 336 respectively.
-
Why did we need a single language, Ada?
Topic: Ada • 2 marks marks
The necessity for a single standard language was felt in 1975. The draft requirements were given the code name Strawman. Strawman was refined to Woodenman and then Tinman in 1976. Initial designs were submitted in 1978 and Red and Green were short listed on the basis of these designs. Standard requirements were then refined to Steelman. The designs were refined additionally and finally in 1979 Green was selected. DoD announced that the language will be called Ada. In 1995 a revision of Ada (Ada 95) was developed by a small team managed by Tucker Taft. In both cases, the design needed a public comment period where the designers answer back to public comments.
-
Which language is the result of history's largest design effort, in which most of the programming is done today?
Topic: Ada
Ada – 1983 – History's largest design effort. It involved a huge design effort, involving hundreds of people, much money, and about eight years. It introduced Packages – support for data abstraction, elaborate exception handling, generic program units, and concurrency through the tasking model.
-
What was the main emphasis of the Ada language?
Topic: Ada • 2 marks marks
Ada is a computer programming language originally designed to support the construction of long-lived, highly reliable software systems. Its design emphasizes readability, avoids error-prone notation, encourages reuse and team coordination, and it is designed to be efficiently implementable.
-
State which of the following statements are valid or invalid in the Ada language: a = (1..5) integer; b = (1..5) integer; c, d = (1...5) float; and the conditions a = b, a = c, b = a.
Topic: Ada • 5 marks marks
In Ada, a constrained (range) subtype is declared as: a : Integer range 1 .. 5; — the source's shorthand a : integer (1..5); is valid in intent: it declares an integer variable whose values are restricted to the range 1 to 5. Conditions: a = b is valid, because a and b have the same type and range. a = c is invalid, because a is an integer type while c is a float type, and Ada's strong typing does not allow comparison/assignment between different numeric types without conversion. b = a is valid for the same reason as a = b. (The source file's answer to this question is garbled/incomplete, so this answer follows standard Ada rules.)
-
Why does Ada not use += and -= type of operators?
Topic: Ada
Such operators reduce readability. So Ada has not included operators like +=, -=, *=, /=, |=.
-
Write the names of the operators which are avoidable in Ada but not in C/C++.
Topic: Ada
PlusEquals += SubtractEquals -= MultiplyEquals *= DivisionEquals /= OrEquals |= AndEquals &=
-
Write a small program in Ada to print "BS-computer-sciences".
Topic: Ada
with Ada.Text_IO; use Ada.Text_IO; procedure Name is begin New_Line; Put("BS-computer-sciences"); end Name;
-
Write a "Hello World" program in Ada.
Topic: Ada
with Ada.Text_IO; use Ada.Text_IO; procedure Hello_World is begin Put_Line("Hello World!"); end Hello_World;
-
Write a simple Ada program to check whether a number is even or not.
Topic: Ada
declare N : Integer := 5; begin if N rem 2 = 0 then Put_Line ("Even number"); elsif N rem 2 /= 0 then Put_Line ("Odd number"); else Put_Line ("Something went really wrong!"); end if; end;
-
Write an Ada program for the given pattern using a for loop, using the Put() method instead of Put_Line() and New_Line for a new line: * / ** / *** / **** / *****
Topic: Ada
procedure Loopprint is Number_Of_Time : Integer := 5; begin for I in 1 .. Number_Of_Time loop for J in 1 .. I loop Put ('*'); end loop; New_Line; end loop; end Loopprint;
-
Briefly explain the data structures that are used in Ada for: (a) Encapsulation (b) Defining extensible types.
Topic: Ada • 5 marks marks
Encapsulation: The primary structure used for encapsulation in Ada is called a package. Packages are used to group data and subprograms. Packages can be hierarchical, allowing a structured and extensible relationship for data and code. All the standard Ada libraries are defined within packages. Extensible Types: Tagged types are used to define extensible types. Tagged Type: A tagged type is like a record which can be used to declare objects. Following is an example of a tagged type: type Person is tagged record Name : String(1..20); Age : Natural; end record;
-
What is a tagged type in Ada?
Topic: Ada
Tagged Type: A tagged type is like a record which can be used to declare objects. Following is an example of a tagged type: type Person is tagged record Name : String(1..20); Age : Natural; end record;
-
Type a record named School using the Ada language.
Topic: Ada
type School is record Name : String; Address : String; Phone_Number : String; Principal_Name : String; Number_of_Students : Integer; end record;
-
Write code in Ada where the parent class is Employee and the child class is Teacher, and explain it.
Topic: Ada
with Ada.Text_IO; use Ada.Text_IO; type Employee is tagged null record; procedure Print_Name (Self : in out Employee); type Teacher is new Employee with null record; procedure Print_Subject (Self : in out Teacher); procedure Employee.Print_Name (Self : in out Employee) is begin Put_Line("Name: John Doe"); end Print_Name; procedure Teacher.Print_Subject (Self : in out Teacher) is begin Put_Line("Subject: Math"); end Print_Subject; Here Employee is a tagged (parent) type and Teacher is derived from it (child type), which is how inheritance is expressed in Ada. (Note: the paper also recalled a second, GUI-tasks question; its answer was not given in the source.)
-
What is the difference between functions and procedures in Ada?
Topic: Ada
In Ada, a function is a subprogram that returns a value, while a procedure is a subprogram that does not return a value. Functions are used to perform calculations or transformations on input data and return the result, while procedures are used to perform actions, such as printing output or modifying the state of a program. Functions have return types while procedures do not. Functions can be used in expressions and can be passed as arguments to other subprograms, while procedures cannot; procedures can only be called. For example, you might use a function to calculate the square of a number, and a procedure to print the result to the screen: function Square (X : Integer) return Integer is begin return X * X; end Square;
-
Does Ada facilitate programmers to do programming as a human activity? If yes, give a reason.
Topic: Ada • 3 marks marks
Yes, Ada believes in programming as a human activity. Programming as a human activity is the main goal of Ada. In Ada the whole philosophy is that a programmer is a human and all humans make mistakes. So to make it safer, Ada uses discriminants in a record that tell what type of data is currently stored there.
-
Why is Ada considered to be a strongly typed language? Briefly explain.
Topic: Ada
Ada is strongly typed. A programming language is strongly typed if type errors are always detected, and in Ada errors can be signaled as exceptions and handled explicitly. Many serious errors such as computational overflow and invalid array indexes are automatically caught and handled through this exception mechanism, improving program reliability.
-
Do small alphabets and capital alphabets are considered the same in Ada? Justify your answer.
Topic: Ada • 2 marks marks
Yes — Ada is not case sensitive: identifiers written in small or capital letters are considered the same.
-
When the Put_Line method is called in Ada, does it need to call the New_Line method or not?
Topic: Ada
No need, because Put_Line already adds a new line. When we use only Put(), then we use New_Line.
-
What do you know about increment/decrement, or predecessors and successors, in enum types in Ada? Explain with a suitable example.
Topic: Ada • 3 marks marks
An enumeration type in Ada is a sequence of ordered enumeration literals, just like C: type Colors is (Red, Indigo, Orange, Green, Yellow, Blue, Violet); type State is (Off, Powering_Up, On); It is however different from C in many respects: there is no arithmetic defined for these types. For example: S1, S2 : State; S1 := S1 + S2; -- Illegal One can however add/subtract one (sort of increment and decrement) using the Pred and Succ attributes, as shown below: State'Pred (S1) State'Succ (S2)
-
Explain operator overloading in Ada and C++.
Topic: Ada
Operator Overloading in Ada: Ada allows a limited overloading of operators. The exception in Ada is that the assignment operator (:=) cannot be overridden. It can be overridden in case of inheritance from a special kind of "abstract class". When you override the equality operator (=) you also implicitly override the inequality operator (/=).
-
Whether polymorphism is allowed in Ada or not? Briefly state.
Topic: Ada • 2 marks marks
Ada 83 supported a form of polymorphism through the use of overloading that allowed the developer to reuse a procedural name or operator by redefining the context and semantics. For example, when using integer numbers, the + sign adds one integer to another. The + operator could be redefined to work with an abstract data type consisting of a set of colors and provide the proper color mix (i.e., red + yellow = orange). This polymorphic behavior had to be resolved at compile time. Ada 95 supports true polymorphic operations through the use of class-wide types and type extensions, which can contextually recognize the appropriate operation to perform.
-
Is there anything like templates as in C++ (in Ada)?
Topic: Ada
Generics are like templates in C++ and allow parameterization of subprograms and packages with parameters which can be types and subprograms as well as values and objects.
-
Write an array in Ada using the days of a week as indexes.
Topic: Ada
For example, if we define Days as below: type Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Then we can create an array which uses Days (or any sub-range of Days) as the array indexes, as shown below: type Daily_Sales is array (Days) of Float; Now the type Daily_Sales is an array type of size 7 and indexes Monday through Sunday. Also note that these literals will be used as indexes in the statements that reference array elements.
-
How are Bitwise Exclusive OR and NOT represented in Ada?
Topic: Ada
Bitwise Exclusive Or = xor Bitwise Not = not Logical Not = not
-
Suppose you have to write a program in the Ada language for calculating marks of a student for 3 subjects. The total marks for each subject are: Subject1 = 100, Subject2 = 200, Subject3 = 400. How will you restrict users to not enter values less than 0 and greater than the total marks? Also write a code snippet for this. Note: The use of loops and conditions is not allowed.
Topic: Ada
The restriction is imposed through range-constrained subtypes, so no loops or conditions are needed — any out-of-range value raises Constraint_Error automatically: subtype Marks1 is Integer range 0 .. 100; subtype Marks2 is Integer range 0 .. 200; subtype Marks3 is Integer range 0 .. 400; S1 : Marks1; S2 : Marks2; S3 : Marks3; When a value is read into S1, S2 or S3, Ada checks it against the declared range; a value less than 0 or greater than the subject's total marks is rejected at run time by the language itself.
-
Write two distinct features of ALGOL which were not present in FORTRAN.
Topic: Language History & Evolution
1. Block structure: ALGOL introduced block structure with local scope (begin ... end compound statements), which was not present in FORTRAN. 2. Recursion: ALGOL supported subprogram recursion, along with stack-dynamic arrays whose size is defined at run time. It also provided two parameter passing methods – pass by value and pass by name.
-
Why was COBOL encountered with problems at the start?
Topic: Language History & Evolution • 2 marks marks
COBOL encountered problems at the start because the design committee members were all from computer manufacturers and DoD branches, and there were fights among the manufacturers.
-
What will be the choice of language in the functional programming paradigm?
Topic: LISP
Functional programming is a style of programming that emphasizes the evaluation of expressions, rather than execution of commands. The expressions in these languages are formed by using functions to combine basic values. A functional language is a language that supports and encourages programming in a functional style. LISP is a representative language of this style of programming.
-
Name two special symbols used for logical true and false in LISP.
Topic: LISP
There are two special symbols in LISP: T for logical true and NIL for logical false.
-
Which language firstly used recursion?
Topic: LISP • 2 marks marks
LISP was the first language to use recursion.
-
Which symbol is used for alternation in SNOBOL?
Topic: SNOBOL
The vertical bar is used to specify pattern alternation, as shown in the example below: P1 P2 This is an example of a pattern that will match either P1 or P2.
-
In SNOBOL, what is used in place of a pointer?
Topic: SNOBOL
The unary operator '$' is used for indirect reference in SNOBOL. Indirect reference is an interesting feature of SNOBOL and is similar to a pointer in concept.
-
Explain immediate assignment and conditional assignment in SNOBOL4.
Topic: SNOBOL
The '.' (dot) operator is used for conditional assignment, only when the entire pattern is matched. The '$' operator is used for immediate value assignment, even if the entire pattern does not match. It is used as follows: Pattern $ Variable
-
Write the resultant output after evaluating the following arithmetic expressions using SNOBOL: a) 5**2**2 b) -56+7 c) 5**7+2 d) 7/2
Topic: SNOBOL
In SNOBOL, exponentiation (**) is right-associative and arithmetic on integers produces integer results. a) 5**2**2 = 5**(2**2) = 5**4 = 625 b) -56+7 = -49 c) 5**7+2 = 78125+2 = 78127 d) 7/2 = 3 (integer division)
-
Consider the following code segment in SNOBOL language: N = '5'; S = 'N'; 'SEAT5N' N S. How will the concatenation and pattern matching operations take place in the above code segment?
Topic: SNOBOL
In the statement 'SEAT5N' N S, the subject is the string 'SEAT5N'. The pattern is formed by concatenating the values of N and S, i.e. '5' and 'N', which gives the pattern '5N'. This pattern is then searched in the subject 'SEAT5N'. Since '5N' is present at the end of the subject, the pattern matching will be successful.
-
Write two data types used in SNOBOL.
Topic: SNOBOL
Two data types used in SNOBOL are Integer and String. SNOBOL also supports other data types such as Real, Pattern, Array and Table.
-
In SNOBOL4, create a table of unspecified length.
Topic: SNOBOL • 3 marks marks
A table of unspecified length is created by calling the TABLE function without giving a size: T = TABLE() Such a table starts with a default size and grows automatically as more entries are added to it.
-
Tell the names of operators that are present in Ada but not in C++.
Topic: Ada
Remainder: rem Absolute Value: abs Exponentiation: ** Range: .. Membership: in String Concatenation: &
-
Differentiate between Ada access types and C/C++ pointer type.
Topic: Ada • 5 marks marks
An access type roughly corresponds to a C++ pointer. type Address_Ref is access Address; A_Ref := new Address; A_Ref.Postal_Code := "94960-1234"; Note that, unlike C, there is no notational difference for accessing a record field directly or through an access value. To refer to the entire record accessed by an access value, the following notation is used: Print(A_Ref.all);
-
Differentiate between C/C++ unions and Ada discriminated type.
Topic: Ada • 3 marks marks
Discriminated records are like union types in C. There are, however, major differences between C union types and Ada discriminated records. The union type in C is fundamentally unsafe and therefore unacceptable. For example, in a union of int and float, at runtime the compiler and the environment cannot tell which particular value has been stored currently, so we have to trust the programmer. In Ada we are short on trust: the whole philosophy is that a programmer is a human and all humans make mistakes. So, to make it safer, Ada uses discriminants in a record that tell what type of data is currently stored there.
-
Why does Ada not use '=' operator for assignment like other languages?
Topic: Ada • 3 marks marks
Like all imperative languages, Ada also supports the assignment statement. However, in Ada the assignment is not an expression like C. In Ada, ':=' is used as the assignment operator, whereas '=' is used for the equality test. The syntax of the assignment statement is: Variable := expression;
-
What will be the result of the following Ada code? A := 8; B := 6; if A > B then A := 5 else A := A - 3 end if;
Topic: Ada • 2 marks marks
Output = 5. Since A (8) is greater than B (6), the condition A > B is true, so the then part is executed and A is assigned the value 5.
-
Write an Ada case statement for a variable TODAY of an enumeration type (MON, TUE, WED, THU, FRI, SAT, SUN) that performs WORK on MON to THU, WORK and PARTY on FRI, and REST on SAT and SUN.
Topic: Ada
case TODAY is when MON .. THU => WORK; when FRI => WORK; PARTY; when SAT SUN => REST; end case;
-
Suppose you have defined your own integer data type ranging from 0 to 1000 in Ada. If anyone else declares a variable using your data type and gives a value more than 1000 to the variable, will the compiler generate an error? If yes, then explain why.
Topic: Ada
Yes. Ada is a strongly typed language and performs range constraint checking. A variable of a type defined with the range 0 .. 1000 can only hold values within this range. Assigning a value greater than 1000 violates the range constraint, so the error is detected and a Constraint_Error exception is raised.
-
Type BIG is mod 8; big1 : BIG := 3; Is there any error in this Ada code?
Topic: Ada
No, there is no error. 'mod 8' defines a modular integer type whose values range from 0 to 7. The value 3 lies within this range, so the declaration and initialization of big1 are valid.
-
From the following code snippet in Ada, which statement will execute if A contains 12? (2 marks) case A is when 1 => fly; when 11 | 14 => null; when 22 | 20..30 => ...; when others => complain; end case;
Topic: Ada
The value 12 does not match the choice 1, the choices 11 14, or the range 20..30. Therefore the 'others' choice is selected and the statement 'complain' will be executed.