Computer Knowledge
Programming Languages and Compilers
2,284 Questions
Programming languages and compilers involve the rules, syntax, and semantics used to write and execute software programs. Key areas include scripting languages, object oriented concepts, and parsing algorithms like top down parsers. Practice these computer science questions to build proficiency for technical and computer knowledge exams.
Object oriented languagesScripting languagesCompilers and parsersProgramming syntax
Programming Languages and Compilers Questions
-
REPEAT 5 TIMES;instruction;END;
-
DO J=1 TO 5;instruction;END;
-
COUNTER = 1;DO WHILE COUNTER < 5;COUNTER = COUNTER + 1;instruction;END;
-
REPEAT VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER = 5;instruction;END;
B
Correct answer
Explanation
PL/I uses the DO loop construct for iteration control. The correct syntax for repeating an instruction 5 times is DO J=1 TO 5; instruction; END; which creates a controlled loop with an index variable. The REPEAT keyword doesn't exist in PL/I, and the WHILE option would require different initialization and would execute only 4 times with < 5 condition.
-
after each declaration of a variable
-
at the end of each instruction
-
to end a DO instruction
-
at the end of the program
-
to end an IF instruction
B,D,E
Correct answer
Explanation
In PL/I, semicolons terminate statements rather than acting as separators. Thus, a semicolon is required at the end of each statement (instruction), to terminate groups like DO, and to terminate the program block. However, the options listed are slightly confusing as a DO instruction ends with a separate END statement, which contains a semicolon, but 'at the end of each instruction', 'at the end of the program', and 'to end an IF instruction' (which is terminated by a semicolon) align with PL/I statement terminator rules.
A
Correct answer
Explanation
The exit() function is part of the C Standard Library, specifically in the cstdlib header (stdlib.h in C). It's used to terminate program execution and return a status code to the operating system. This is a standard function used throughout C and C++ programs for controlled program termination.
A
Correct answer
Explanation
A variable can be declared both const and volatile. This is useful for hardware registers that can change externally (volatile) but shouldn't be modified through the program (const). Example: volatile const int *ptr for memory-mapped I/O.
B
Correct answer
Explanation
COBOL and PASCAL are both high-level programming languages, not computer viruses. COBOL (Common Business-Oriented Language) is used in business and finance applications, while PASCAL was designed for teaching programming concepts. Viruses are malicious software that replicate themselves, which these languages are not.
-
SCALAR
-
empty value
-
STRING
-
"not a reference"
B
Correct answer
Explanation
The ref function returns a true value (indicating the reference type) only if its argument is a reference. Since $val holds a plain string ('x') and not a reference, ref returns an empty string (empty value).
-
A
-
B
-
the code will fail
-
None of the above
A
Correct answer
Explanation
Calling B->new() inherits new from package A. In A::new, the single-argument bless {} blesses the object into the current package (A). Thus, \$obj is an instance of A, and \$obj->foo() calls A::foo which returns 'A'.
-
empty string
-
value
-
the code will fail
-
None of the above
C
Correct answer
Explanation
The code fails because init() is called before bless() returns a fully constructed object. When init() attempts $self->{key} = 'value', $self is still an unblessed hash reference (not yet an object of class A), but more critically, calling init() inside new() before the bless completes means $self lacks proper object context. Perl's bless() operation must complete before the object can properly call its own methods. The correct pattern is to bless first, then initialize, or use a post-bless initialization hook.
-
2
-
6
-
106
-
I don't like undefs
C
Correct answer
Explanation
The array @a contains (1, undef, 2). The loop iterates three times: first val=1, foo(1) returns 2, sum becomes 2; second val=undef, foo(undef) dies with error message, eval catches it ($@ is true), so sum += 100 making sum = 102; third val=2, foo(2) returns 4, sum becomes 106. The print outputs 106. The eval block traps the die() on undefined input, adding 100 instead of failing.
-
undef
-
0
-
1
-
2
-
code will fail
C
Correct answer
Explanation
When Perl evaluates exists $var->{key1}->{key2}, it autovivifies (auto-creates) the intermediate hash structure even though the key2 doesn't exist. This means $var->{key1} is created as an empty hash. The exists check returns false (key2 doesn't exist), so the assignment inside the if block doesn't execute. However, the autovivication already happened, leaving $var with one key (key1). keys(%{$var}) returns 1.
-
abcd
-
acbd
-
cadb
-
code will fail
C
Correct answer
Explanation
BEGIN blocks execute at compile time in declaration order, while END blocks execute at runtime in reverse declaration order (LIFO - last in, first out). The two BEGIN blocks print 'a' then 'c'. The two END blocks are declared in order (prints 'b' then 'd'), but execute in reverse order, so 'd' prints before 'b'. Final output: 'ac' from BEGIN blocks, then 'db' from END blocks, yielding 'cadb'.
A
Correct answer
Explanation
The statement assigns a list to a list, evaluating the right-hand side in list context. The array elements are copied, and since the left-hand side lists only one variable ($b) inside parentheses, it receives the first element of the array, which is 0.
-
(A) Sections
-
(B) Paragraphs
-
(C) Sentences
-
(D) Statements.
A
Correct answer
Explanation
In COBOL Procedure Division, only Sections are optional. You must have at least one Paragraph, which contains Sentences, which are composed of Statements. This hierarchy is: Sections (optional) > Paragraphs (required) > Sentences (required) > Statements (required). A procedure can consist of a single paragraph with sentences, without any sections, making sections the only truly optional element.
-
(A) Start
-
(B) Rewrite
-
(C) Delete
-
(D) Update.
D
Correct answer
Explanation
In COBOL, START, REWRITE, and DELETE are valid input/output verbs used to perform operations on files. UPDATE is not a valid COBOL verb; modifying an existing record is accomplished using the REWRITE verb. Thus, option D is the correct choice.
-
Math.round(Math.random() * (high - low)) + low
-
Math.round(Math.random())
-
Math.round(Math.random())+ low
-
None of the baove
A
Correct answer
Explanation
Math.random() returns a value in the range [0, 1). Multiplying it by the range size (high - low), rounding the result using Math.round(), and shifting it by adding low correctly generates a random integer in the closed interval [low, high]. Other choices fail to scale or shift the output correctly.