Skip to main content

Programming Language Concepts Questions/Answers Part 3

1. What is an associative array?
- An unordered collection of data elements that are indexed by keys.

2. Each element of an associative array is a pair consisting of a _______ and a _______.
- key and a value

3. True or False? Java supports associative arrays?
- True. As a matter of fact, Perl, Python, Ruby, C++, C# and F# do too.

4. What are associative arrays called in Perl?
- hashes

5. Why are associative arrays in Perl called hashes?
- Because their elements are stored and retrieved with a hash function

6. What character does a hash in Perl begin with?
%

7. In Perl, each key is a _____ and each value is a _______.
- string
- scalar

8. In Perl, subscripting is done using _______ and _______.
- braces and keys

9. In Perl, how are elements removed from hashes?
- using delete

10. In Perl, the ________ operator tests whether a particular value is a key in a hash.
- exists

11. What are associative arrays called in Python?
- dictionaries

12. What is a difference between Python’s dictionaries and Perl’s hashes?
- The values in Python are references to objects. In Perl they’re scalars.

13. What’s the difference between Ruby’s associative arrays and Perl’s?
- Ruby’s keys can be objects. In Perl they can only be strings.

14. Does PHP have associative arrays?
- Yes

15. In PHP, a subscript can be an ______ or a _______.
- integer or a string

16. What’s is Lua’s only data structure?
- associative arrays

17. What is a record?
- a data structure in which individual elements are identified by name

18. Records were introduced in what programming language?
- COBOL

19. In Object Oriented Programming Languages, records are simulated with ________.
- objects

20. In C, C++ and C#, records are supported with the _______ data type.
- struct

21. What’s the difference between a record and an array?
- Record elements (fields) are referenced by name instead of indices.

22. In Java and C#, records can be defined as _______.
- classes

23. Lua’s ______ can be used as records.
- tables

24. What are the two ways to reference elements of a record?
- Full Qualified Notation and Elliptical Reference

25. Describe the full qualified notation
- must include all record names

26. Describe elliptical references
- allow leaving out of record names if the reference is unambiguous

27. Elliptical references allow leaving out of record names if the reference is _________.
- unambiguous

28. How are fields of a record stored in memory?
- in adjacent memory locations

29. How are record fields accessed in memory?
- through offset

30. The ________ associated with a field specifies the distance from the beginning of the record to the beginning of the field.
- offset

31. True or False? Run-time descriptors of records are necessary.
- False

32. What is a tuple?
- a data type that’s like a record except that elements are not named.

33. Python includes an _________ tuple type.
- immutable

34. How do you change a tuple in Python?
- by using the list function to convert it to a list
- after the change use the tuple function to convert the list back to a tuple

35. How do you create a tuple in Python?
- myTuple = (3, 5, 8, ‘apple’)

36. How do you access elements of a tuple in Python?
- indexing i.e. myTuple[1]

37. In Python, tuples can be concatenated with the ______ _________.
- plus operator

38. In Python, tuples can be deleted with the ____ statement.
- del

39. True or False? Python’s tuples can be empty.
- True

40. True or False? ML’s tuples can be empty.
- False. Must have at least 2 elements

41. True or False? Python and ML can include elements of mixed types.
- True

42. How can tuples be defined in ML?
- With the type declaration. i.e. type intReal = int * real;

43. How do you create a tuple in F#?
- With the let statement. i.e. let tup = (3, 5, 7);;

44. How can you access the first two elements of a tuple in F#?
- with the functions fst and snd.

45. Why are tuples used in Python, ML and F#?
- to allow function to return multiple values

46. ______ in Scheme and Common Lisp are enclosed in parentheses.
- Lists

47. True or False? In a Scheme list, elements are separated with a comma.
- False. No punctuation

48. True or False? Lists can be nested.
- True (A (B C) D)

49. Name two ways that the following list can be interpreted as: (A B C)
- If interpreted as code, it’s a call to the function A with parameters B and C
- If interpreted as data, it’s a list with 3 elements: A, B and C.

50. How to prevent a list from being treated as a function call?
- with a quote i.e. (CAR ‘ (A B C) ) ….returns A

51. In Scheme and Common Lisp, the ____ function returns the first element of a list.
- CAR

52. In Scheme and Common Lisp, the _____ function returns the list minus its first element.
- CDR . i.e. (CDR ‘ (A B C) ) returns (B C)

53. How are new lists constructed in Scheme and Common Lisp?
- With CONS and LIST functions.

54. _______ returns a new list with its first parameter as the first element and its second parameter as the remainder of that list.
- CONS i.e. (CONS ‘A ‘(B C)) returns (A B C)

55. ______ takes any number of parameters and returns a new list with the parameters as the elements.
- LIST i.e. (LIST ‘A ‘B (C D)) returns (A B (C D))

56. In ML, lists are specified in ______ ________, with the elements separated by ________.
- square brackets
- commas

57. Name two ways to represent an empty list in ML.
- [] or nil

58. True or False? In ML, elements of a list can be of different types.
- False

59. Name the ML alternatives to CAR and CDR.
- hd (head) i.e. hd [5, 7, 9] is 5
- tl (tail) i.e. tl [5, 7, 9] is [7, 9]

60. True or False? Lists in Python are mutable.
- True

61. True or False? In Python, elements in a list can be of a different type.
- True

62. ______ ___________ are a powerful mechanism for creating lists.
- List comprehensions

63. List comprehensions are derived from _____ notation.
- set

64. In which programming language did list comprehensions first appear?
- Haskell

65. What does a list comprehension do?
- applies a function to each element of a list and constructs a new list from the results.
- i.e. in Python [x * x for x in range(12) if x % 3 == 0] produces [0, 9, 36, 81]

66. What is a union?
- a type whose variables can store different type values at different times during execution

67. What are free unions?
- the unions in C and C++ where there is no language support for type checking

68. What are discriminant unions?
- A union with a discriminant where each union includes a type indicator, knows as a tag or discriminant.

69. Write an example of a union in C.
- union {
        int I;
        float f;
  }u;
So you can write:
float x;
….
u.i = 27;
x = u.f;

70. True or false? Unions in C and C++ are strongly typed?
- False

71. True or false? Java and C# provide safe unions.
- False. They don’t include unions at all

72. How are unions implemented?
- by assigning the same address to every possible variant

73. _____ type values consist of memory addresses and the special value ‘nil’
- Pointer

74. What were pointers designed to do?
- Indirect addressing
- Accessing anonymous variables in the heap

75. What are the two operations that programming languages provide that have pointer types?
- Assignment
- Dereferencing

76. What does the assignment operation do?
- Sets a pointer variable’s value to some useful address

77. If pointers are used only to manage dynamic storage, the pointer variable is initialized by an _______ ________.
- allocation mechanism

78. If pointers can point to variables that are not heap-dynamic, then there must be an _______ or _______ _________ that fetches the address of a variable.
- operator or built-in subprogram

79. Accessing the memory cell a pointer points to is called ________ the pointer.
- dereferencing

80. Dereferencing can be either _____ or _______.
- explicit or implicit

81. In most programming languages, dereferencing is done _______.
- explicitly

82. How do you dereference a pointer in C++?
- explicitly using the (*) as a prefix unary operation. i.e. j = *ptr

83. If a pointer points to a structure in either C or C++, how can you access a field named age?
- i.e. (*p).age or p->age

84. What does -> do?
- Combines dereferencing and field reference.

85. Languages that provide pointers for heap allocation must include an _____ ______ ________.
- explicit allocation operation

86. Name one operation in C used for heap allocation.
- malloc

87. What was the first high-level programming language to include pointer variables?
- PL/I

88. True or False? Pointers in PL/I can refer to both heap-dynamic variables and other program variables.
- True

89. What is a dangling pointer?
- It’s a pointer that contains the address of a heap-dynamic variable that has been deallocated.

90. Why are dangling pointers dangerous?
- The location being point to may have been reallocated to some new heap-dynamic variable.

91. What is a lost heap-dynamic variable?
- A dynamic variable that is no longer accessible to a program

92. What’s another term for lost-heap dynamic variables?
- garbage

93. Why are lost heap-dynamic variables sometimes referred to as garbage?
- because they’re no longer useful, but the space they occupy cannot be reallocated

94. The problem of lost heap-dynamic variables is sometimes called ____ ______.
- memory leakage

95. True or False? Pointers in C can point to any variable.
- True

96. How do you get the address of a variable in C?
- with the ampersand symbol (&)

97. If the pointer is pointing to an array, what are the 3 forms of pointer arithmetic supported in C and C++?
- Adding an integer to a pointer
- Subtracting an integer from a pointer
- Subtracting two pointers

98. True or false? An array name not followed by subscripts can be used as a pointer to the first element of the array.
- True
i.e.
int list[10];
int *ptr;

ptr = list;

so you can do

*(ptr + 1)
*(ptr + index)
ptr[index]

99. True or False? In C and C++, pointers can point to a function.
- True

100. C and C++ allow pointers of type ______, which can point at values of any type.
- void *

101. What is the return type of the function malloc?
- void *

102. In Java, a value of a reference type is like a pointer, except that it must refer to an ______.
- object

103. True or false? Arithmetic cannot be performed on references.
- True

104. What is a reference in C++?
- an alias for another variable

105. True or false? A reference type in C++ must be initialized at the time it’s bound to storage and cannot refer to any other variable during its lifetime.
- True

106. What are reference types mainly used for?
- Declaring parameters

107. True or False? C# includes both the reference of Java and the pointer of C++.
- True

108. For a subprogram to use a pointer in C#, you must include the _____ modifier.
- unsafe

109. True or false? All variables in Smalltalk, Python, Ruby and Lua store references and are implicitly dereferenced.
- True

110. Name one area where pointers are beneficial.
- writing device drivers

111. Name two proposed solutions for the dangling pointer problem.
- the use of tombstones
- the use of locks-and-keys

112. What is a tombstone?
- Pointer variables point to a tombstone cell that in turn points to the heap-dynamic variables.

113. True or false? When the heap-dynamic variable is deallocated, the tombstone remains.
- True but it’s set to nil.

114. What is the locks-and-keys approach?
- A pointer is stored as a (key, address) pair where the key is an integer value and each heap-dynamic variable has a header cell that store an integer lock value. When the heap-dynamic variable is allocated, a lock value is created and placed both in the lock cell of the heap-dynamic variable and in the key-cell of the pointer. When the pointer is dereferenced, the key value of the pointer is compared to the lock value of the heap-dynamic variable.

115. What is the best solution to the dangling pointer problem?
- To provide implicit deallocation for heap-dynamic variables that are no longer useful

116. How does Java take care of the deallocation of heap-dynamic variables?
- Implicitly using their garbage collector.

117. _______ are the fundamental means of specifying computations in programming languages.
- Expressions

118. Operator evaluation is governed by __________ and ___________ rules.
- associativity and precedence

119. Most of the characteristics of arithmetic expressions in programming languages were inherited from ________.
- mathematics

120. In programming languages, arithmetic expressions consist of what?
- operators, operands, parentheses and function calls

121. What are the three types of operators
- Unary: one operand
- Binary: two operands
- Ternary: three operands

122. In most programming languages, binary operators are _______, appearing between their operands.
- infix

123. Unary operators can be either ______ or _______.
- prefix or postfix

124. True or false? The value of an expression may depend on the order of evaluation of the operators in the expression.
- True

125. The operator precedence rules of the common language are nearly all the same and include:
- 1. Exponents has the highest precedence
- 2. Multiplication and division have lower precedence than 1
- 3. Addition and subtraction have lower precedence than 2.

126. What is a unary plus operator called?
- identity operator

127. Why is the unary plus operator called the identity operator?
- Since it usually has no effect

128. True or False? In Java, unary plus and minus cause the implicit conversion of short and byte operands to int type.
- True

129. Which language has a single level of precedence?
- APL

130. When an expression contains adjacent occurrences of operators with the same precedence, __________ rules determine which operator is associated first.
- associativity

131. An operator can either have ______ or ______ associativity.
- left or right.

132. True or False? Visual Basic’s exponentiation operator is left associative.
- True. Most languages are right associative

133. True or False? In APL, the order of operator evaluation is determined entirely by associativity, which is right to left for all operators.
- True

134. Why are floating point numbers often not associative?
- Overflow problem

135. How do you override normal precedence and associativity rules?
- parentheses

136. How are arithmetic and relational operators implemented in Ruby?
- as methods since Ruby is a purely Object-Oriented language

137. True or false? Operators in Ruby can be overridden.
- True

138. In Lisp, operators are _______.
- functions

139. What’s the ternary operator used for?
- to form conditional expressions in C based languages

140. What is a side-effect?
- the result when an operator or function call performs some other action in addition to returning a value i.e. int j = ++i + i; What is j? depends on the associativity

141. A program has the property of ______ ______ if any two expressions with the same value can be substituted for each other without affecting the program’s behavior.
- referential transparency

142. What is operator overloading?
- Allowing the operator to have multiple meanings

143. What happens when an overloaded operator is used in an expression?
- The compiler examines the types of operands to determine the correct meaning of the operator

144. Type conversions are either _______ or ________ and can be explicit or implicit.
- narrowing or widening

145. What is a narrowing conversion?
- converts a value to a type that cannot store even approximations of all the values of the original type. i.e. int to byte

146. What is a widening conversion?
- converts a value to a type that can include at least the approximations of all the values of the original type. i.e. int to double

147. True or false? Narrowing conversions are safe.
- False

148. What are implicit type conversions called?
- coercion

149. What are explicit type conversions called?
- casts

150. What is a mixed-mode conversion?
- operators can have operands of different types

151. Why does Java coerce byte variables into integers before an arithmetic operation is performed?
- architecture. There’s a way to add integers together, but not bytes.

152. Why are casts placed in parentheses?
- because type names may consist of more than one word: i.e. long double

153. When does overflow occur?
- When the result of an operation is too large to store in the designated amount of memory

154. When does underflow occur?
- When the result of an operation is too small to represent.

155. What is another word for a run-time error?
- exception

156. True or false? In Java, floating point division by zero is an error.
- False. Infinity is a well-defined value

157. A _______ ________ compares the values of its two operands.
- relational operator

158. What is the value of the relational expression normally?
- Boolean

159. What is the purpose of === operator in PHP?
- the “is identical to” operator prevents type coercion
- i.e. “7” == 7 is true but “7” === 7 is false

160. How to prevent coercion in the relational expression In Ruby?
- instead of using ==, use eql?

161. True or False? C based languages give OR a higher precedence than AND.
- False. AND gets higher precedence than OR

162. Prior to C99, C had no _______ type. Instead, numbers were used.
- Boolean

163. In C, ______ is considered false.
- zero (all other values represent true)

164. With _______ ________ _________, the value of an expression is determined without evaluating all of its sub-expressions.
- short-circuit evaluation

165. When can a short-circuit expression cause an issue?
- If a Boolean expression contains a side-effect, the side-effect may not occur

166. The ______ ______ _______ combines assignment with some other operation.
- compound assignment operator (i.e. sum+=value)

167. Which language introduced compound assignment operators?
- ALGOL 68

168. True or False? The expression -count++ is interpreted as –(count++), not (-count)++.
- True

169. Assignments are ______ associative.
- right

170. True or False? Java and C# allow only Boolean expressions in if statements.
- True

171. Give an example of a multiple-source assignment statement in Perl.
- ($first, $second, $third) = (20, 40, 60);

172. In ML, how are names bound to values?
- using val (i.e. val cost = quantity * price;)

173. True or false? Java and C# allow mixed-mode assignments if the required coercion is either widening or narrowing.
- False. Only widening

174. What are the two capabilities that most programming languages need?
- Select from among alternative control flow paths
- Repeatedly execute sequence of statements

175. What are the statements that provide the 2 capabilities called?
- Control statements

176. What is a control structure?
- a control statement and the collection of statements whose execution it controls

177. ________ and ________ proved in 1966 that all algorithms could be expressed using only two control statements: one for choosing between two control flow paths and one for logically controlled iteration.
- Bohm and Jacopini

178. What is the unconditional branch statement called?
- goto

179. True or false? Multiple exits from a control structure pose a danger.
- False

180. A ______ ________ provides a way of choosing between two or more execution paths in a program.
- selection statement

181. What are the two general paths that selection statements fall into?
- two way and n-way

182. If parentheses are omitted in control expressions, what is normally used?
- a keyword marker such as then

183. In Ruby, the entire if construct is terminated by the word _____.
- end

184. How does Python specify compound statements?
- by using indentation

185. In Java, the else clause is always paired with the _______ previous unpaired then clause.
- nearest

186. True or False? In the functional languages ML, F# and Lisp, a selector is not a statement; it is an expression that produces a value.
- True

187. In F#, when an if construct has no else clause, the then clause must return a value of the ____ type. This type has only one value, which is written _________.
- unit
- ()

188. A _____-__________ statement allows the selection of one of any number of statements or statement groups.
- multiple-selection

189. What is an example of a multiple-selection statement in C#?
- switch

190. To logically separate code segments in a switch, an _______ _______ much be used; C’s _____ statement is an example and exits the switch statement
- explicit branch
- break

191. True or False? In C#, implicit execution of more than one segment of a switch statement is allowed.
- False. Only one.

192. In C#, the last statement in each selectable segment ends with either a _____ or a _____.
- break
- goto

193. True or False? C# allows the control expression and case labels to be strings.
- True

194. True or False? In PHP’s switch statement, the case values can be any scalar types.
- True

195. Ruby’s multiple-selection constructs are called _____ ________.
- case expressions

196. True or false? Perl, Python and Lua do not have multiple-selection statements.
- True

197. What is the best implementation technique of a multiple-selection statement?
- branch table (or jump table)

198. In a branch table, the ______ of the table are the case values.
- indices

199. In a branch table, the _____ of the table are the segment values.
- elements

200. How are cases matched in a branch table?
- array subscripting

201. Name two other ways to implement a multiple selection statement.
- binary search and hash table

202. What is Scheme’s multiple selector called?
- COND

203. In Scheme, when using COND, the predicates are evaluated in order until one evaluates to ____.
- #T

204. True or False? When a Scheme COND expression that follows the predicate is evaluated it is returned as the value of COND.
- True

205. True or False? In none of Scheme’s COND predicates are true, and there is an ELSE statement, it’s expression is evaluated and the value is returned?
- True

206. What happens when none of the predicates are true and there is no ELSE in Scheme’s COND?
- COND returns an unspecified value

(COND
     ((> x y) "x is greater than y")
     ((< x y) "y is greater than x")
     (ELSE "x and y are equal")
)

207. An ______ _______, or loop, causes a statement or collection of statement to be executed zero, one or more times.
- iterative statement

208. The ______ of a loop is the collection of statements whose execution is controlled by an iterative statement.
- body

209. What is a pretest loop?
- a loop that tests the condition for loop completion before the loop body is executed.

210. What is a posttest loop?
- a loop that tests the condition for loop completion after the loop body is executed.

211. A counter controlled loop has a ______ ________ in which the count is stored.
- loop variable

212. What are the three parameters that the counter controlled loop also has?
- Initial value of the loop variable
- Terminal value of the loop variable
- Step size – the difference between sequential value of the loop variable

213. What do the three expression in C’s for loop represent?
- First expression: initialization
- Second expression: loop control test
- Third expression: Often used to increment loop counter since it’s evaluated after each execution of the loop body

214. True or False? In C, the comma operator can be used to join multiple expressions into a single expression.
- True

215. True or False? C’s for statements often have empty bodies since all the work can be done in the controlling expressions.
- True

216. True or False? C99 can have a declaration in its first expression.
- True

217. True or false? In pure functional languages, content counters are used for iteration.
- False. Do not contain counter variables and recursion is used instead of iteration

218. What does the reserved word rec indicate in F#’s function declaration?
- That the function is recursive

219. True or False? Every counting loop can be built with a logical loop, but the reverse is not true.
- True

220. Java’s while and do statements are like those of C and C++, except the control expression must have a ______ type.
- Boolean

221. How can pretest logical loops be simulated in a functional language?
- recursive function

222. In a loop, what is an example of an unlabeled exit?
- break

223. True or False? The exit statements in Java and Perl (i.e. break and last) can specify a label.
- True

224. What does the continue statement do in a loop?
- Skips the rest of the loop body

225. In iteration based data structures, an iterative statement uses an _______ to traverse the elements of a data structure.
- iterator

226. True or False? In PHP, each array contains an internal pointer to its “current” element.
- True

227. What does the current function return in PHP?
- the element that is currently being pointed to by the internal pointer

228. What is Java’s solution to iteration over elements in an array?
- enhanced for loop

229. In C#, the iterator can be accessed through the _______ statement.
- foreach

230. True or False? C# users can define their own collections and write their own iterators.
- True

231. In Ruby, a _____ is a sequence of code delimited by either braces or the do and end reserved words.
- block

232. Is the following code valid in Ruby and does it produce the result displayed?
>> 4.times {puts "Hey!"}
Hey!
Hey!
Hey!
Hey!
=> 4
- It’s valid and does produce the results displayed

233. Provide an example of using the each statement in Ruby.
>> list = [2, 4, 6, 8]
=> [2, 4, 6, 8]
>> list.each {|value| puts value}
2
4
6
8
=> [2, 4, 6, 8]

234. Instead of a counting loop, Ruby has the _____ method.
- upto
i.e.
>> 1.upto(5) {|x| print x, " "}
1 2 3 4 5

235. True or False? An unconditional branch statement, or goto, transfers control to a specified location in a program.
- True

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Grab the astounding Azure Training in Chennai along with the best DevOps Training and Java training from Infycle Technologies, the best software training institute in Chennai. Call 7504633633 to get the best placement guidance along with the software training for having a lucrative career in the software industry.

    ReplyDelete
  3. wonderful.. I hope you will like this type of informative article in future. I will definitely bookmark your website if you want to know more about the Best Digital Marketing in Meerut .You will have to visit this site.

    ReplyDelete
  4. Thanks for your valuable knowledge because of this i use to updated, thanks for sharing this wonderful article. Buy Instagram Followers Dubai

    ReplyDelete
  5. In the Global Business Network initiated a project to explore the potential future of marine navigation in the Arctic region, driven by the curiosity to understand the implications of such a scenario. This strategic endeavor aimed to anticipate challenges and prioritize tasks effectively, particularly in Arctic conditions. The project's framework involved assessing the level of demand for resources and trade on the vertical axis, juxtaposed with the relative government stability within and outside the region on the horizontal axis study bay, resulting in four distinct scenarios. Despite substantial investments, economic growth for Russia and Canada would remain elusive. The envisioned "Arctic Race" scenario depicted countries disregarding international agreements to safeguard their national interests, intensifying competition for resources amidst climate change-induced resource scarcity and conflicting interests, potentially leading to disorderly outcomes.

    ReplyDelete

Post a Comment

Popular posts from this blog

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition

Creating your own ArrayList in Java

Wanted to show that certain data structures in Java can be created by you. In this example, we'll go ahead and create an ArrayList data structure that has some of the methods that the built in ArrayList class has. We'll create 2 constructors: The default constructor that creates an ArrayList with a default size of 10. Constructor that allows an initial size to be passed to the array. We'll also create a number of methods: void add(Object x);  A method that allows you to place an Object at the end of the ArrayList. void add(int index, Object x);  A method that allows you to place a value at a given location. Object get(int index):  Allows you to retrieve a value of the arrayList array from a given location. int size();  Allows you to get the number of elements currently in the Arraylist. boolean isEmpty();  Tests to see if the Arraylist is empty. boolean isIn(Object x);  A method that sees if a particular object exist in the arrayList. int find(Object x);