94 Programming Language Q&A (P6)

Programming Language Concepts

I think it’s time to start moving into some relatively modern questions.

547. What is an associative array?

– An unordered collection of data elements that are indexed by keys.

548. Each element of an associative array is a pair consisting of a _______ and a _______.

– key and a value

549. True or False? Java supports associative arrays?

– True. As a matter of fact, Perl, Python, Ruby, C++, C# and F# do too.

550. What are associative arrays called in Perl?

– hashes

551. Why are associative arrays in Perl called hashes?

– Because their elements are stored and retrieved with a hash function

552. What character does a hash in Perl begin with?

%

553. In Perl, each key is a _____ and each value is a _______.

– string
– scalar

554. In Perl, subscripting is done using _______ and _______.

– braces and keys

555. In Perl, how are elements removed from hashes?

– using delete

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

– exists

557. What are associative arrays called in Python?

– dictionaries

558. 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.

559. 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.

560. Does PHP have associative arrays?

– Yes

561. In PHP, a subscript can be an ______ or a _______.

– integer or a string

562. What’s is Lua’s only data structure?

– associative arrays

563. What is a record?

– a data structure in which individual elements are identified by name

564. Records were introduced in what programming language?

– COBOL

565. In Object Oriented Programming Languages, records are simulated with ________.

– objects

566. In C, C++ and C#, records are supported with the _______ data type.

– struct

567. What’s the difference between a record and an array?

– Record elements (fields) are referenced by name instead of indices.

568. In Java and C#, records can be defined as _______.

– classes

569. Lua’s ______ can be used as records.

– tables

570. What are the two ways to reference elements of a record?

– Full Qualified Notation and Elliptical Reference

571. Describe the full qualified notation

– must include all record names

572. Describe elliptical references

– allow leaving out of record names if the reference is unambiguous

573. Elliptical references allow leaving out of record names if the reference is _________.

– unambiguous

574. How are fields of a record stored in memory?

– in adjacent memory locations

575. How are record fields accessed in memory?

– through offset

576. The ________ associated with a field specifies the distance from the beginning of the record to the beginning of the field.

– offset

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

– False

578. What is a tuple?

– a data type that’s like a record except that elements are not named.

579. Python includes an _________ tuple type.

– immutable

580. 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

581. How do you create a tuple in Python?

– myTuple = (3, 5, 8, ‘apple’)

582. How do you access elements of a tuple in Python?

– indexing i.e. myTuple[1]

583. In Python, tuples can be concatenated with the ______ _________.

– plus operator

584. In Python, tuples can be deleted with the ____ statement.

– del

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

– True

586. True or False? ML’s tuples can be empty.

– False. Must have at least 2 elements

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

– True

588. How can tuples be defined in ML?

– With the type declaration. i.e. type intReal = int * real;

589. How do you create a tuple in F#?

– With the let statement. i.e. let tup = (3, 5, 7);;

590. How can you access the first two elements of a tuple in F#?

– with the functions fst and snd.

591. Why are tuples used in Python, ML and F#?

– to allow function to return multiple values

592. ______ in Scheme and Common Lisp are enclosed in parentheses.

– Lists

593. True or False? In a Scheme list, elements are separated with a comma.

– False. No punctuation

594. True or False? Lists can be nested.

– True (A (B C) D)

595. 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.

596. How to prevent a list from being treated as a function call?

– with a quote i.e. (CAR ‘ (A B C) ) ….returns A

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

– CAR

598. In Scheme and Common Lisp, the _____ function returns the list minus its first element.

– CDR . i.e. (CDR ‘ (A B C) ) returns (B C)

599. How are new lists constructed in Scheme and Common Lisp?

– With CONS and LIST functions.

600. _______ 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)

601. ______ 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))

602. In ML, lists are specified in ______ ________, with the elements separated by ________.

– square brackets
– commas

603. Name two ways to represent an empty list in ML.

– [] or nil

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

– False

605. 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]

606. True or False? Lists in Python are mutable.

– True

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

– True

608. ______ ___________ are a powerful mechanism for creating lists.

– List comprehensions

609. List comprehensions are derived from _____ notation.

– set

610. In which programming language did list comprehensions first appear?

– Haskell

611. 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]

612. What is a union?

– a type whose variables can store different type values at different times during execution

613. What are free unions?

– the unions in C and C++ where there is no language support for type checking

614. What are discriminant unions?

– A union with a discriminant where each union includes a type indicator, knows as a tag or discriminant.

615. 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;

616. True or false? Unions in C and C++ are strongly typed?

– False

617. True or false? Java and C# provide safe unions.

– False. They don’t include unions at all

618. How are unions implemented?

– by assigning the same address to every possible variant

619. _____ type values consist of memory addresses and the special value ‘nil’

– Pointer

620. What were pointers designed to do?

– Indirect addressing
– Accessing anonymous variables in the heap

621. What are the two operations that programming languages provide that have pointer types?

– Assignment
– Dereferencing

622. What does the assignment operation do?

– Sets a pointer variable’s value to some useful address

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

– allocation mechanism

624. 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

625. Accessing the memory cell a pointer points to is called ________ the pointer.

– dereferencing

626. Dereferencing can be either _____ or _______.

– explicit or implicit

627. In most programming languages, dereferencing is done _______.

– explicitly

628. How do you dereference a pointer in C++?

– explicitly using the (*) as a prefix unary operation. i.e. j = *ptr

629. 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

630. What does -> do?

– Combines dereferencing and field reference.

631. Languages that provide pointers for heap allocation must include an _____ ______ ________.

– explicit allocation operation

632. Name one operation in C used for heap allocation.

– malloc

633. What was the first high-level programming language to include pointer variables?

– PL/I

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

– True

635. What is a dangling pointer?

– It’s a pointer that contains the address of a heap-dynamic variable that has been deallocated.

636. Why are dangling pointers dangerous?

– The location being point to may have been reallocated to some new heap-dynamic variable.

637. What is a lost heap-dynamic variable?

– A dynamic variable that is no longer accessible to a program

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

– garbage

639. 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

640. The problem of lost heap-dynamic variables is sometimes called ____ ______.

– memory leakage

Want more?

P1. 104 Programming Language Q&A
P2. 95 Programming Language Q&A
P3. 123 Programming Language Q&A
P4. 77 Programming Language Q&A
P5. 146 Programming Language Q&A
P6. 94 Programming Language Q&A
P7. 141 Programming Language Q&A

 

Leave a Reply