Programming in D: The Ultimate Guide for Software Engineers Educative Quiz Answers

Get Programming in D: The Ultimate Guide for Software Engineers Educative Quiz Answers

While D has been around for a while, it’s just now starting to see a surge in popularity. D is a multi-paradigm system programming language that combines a wide range of powerful programming concepts. Because D is a re-engineering of C++, many C++ developers have switched to learning D.

In this course, you will start by building a simple “hello world” program. You will then proceed to cover the concepts related to variables, loops, conditional statements, and arrays in order to build up your fundamentals. After addressing the basic concepts, you will then explore more advanced topics like functions, unit testing, and contract programming.

By the end of this course, you will have the foundations in place to start building your own applications in D.

Enroll on Educative

Challenge 1: Fix the Code

Answer:

import std.stdio;

void fixCode(){
    write("Fix Me!");
}

Quiz 1: Getting Started with D

Q1. void is a

  • Function name
  • Module
  • Keyword
  • Part of syntax

Q2. Predict the output of the following code:

import std.stdio;
void main() {

write("Learn");

write("Programming");

write("in");

write("D");
 }
  • Learn
    Programming
    in
    D
  • Learn Programming in D
  • LearnProgramminginD
  • None of the above

Q3. A compiled program runs _______ as compared to executing that program with an interpreter.

  • Slower
  • Faster

Q4. writeln can take more than one argument.

  • True
  • False

Q5. A compilation error is thrown if there is a

  • mismatched parenthesis
  • missing semicolon
  • misspelled keyword
  • All of the above

Quiz 2: Fundamental Types

Q1. The length in bytes of char ____.

  • 1
  • 2
  • 3
  • 4

Q2. The initial value of bool is ____.

  • true
  • false

Q3. The minimum value of short is ____.

  • 32767
  • -32768
  • -2147483648
  • None of these

Q4. .min property can be used to find theinital value of a data type.

  • true
  • false

Q5. size_t is a data type.

  • true
  • false

Challenge 2: Output Variable Values in a Statement

Answer:

import std.stdio; 

void OutputStatement() {
    int amount = 20; 
    double rate = 2.11; 
    write("I have exchanged ", amount," Euros at the rate of ", rate);     
} 

Quiz 3: Variables and User Input

Q1. What is the output of the following code?

import std.stdio;

void main() {
    char conversionRate;

    // Assigning the value 200.20 to the conversionRate variable:
    conversionRate = 200.20;

    writeln("Today's convertion rate is ", conversionRate); 
}
  • Today’s conversion rate is 200.20
  • Today’s conversion rate is conversionRate
  • An error will be thrown
  • None of the above

Q2. auto can be used with any type even when the type is not spelled out on the right- hand side.

  • True
  • False

Q3. What is the output of the following code?

import std.stdio;

void main() {
    double value;
    typeof(value) convertionRate;


    // Assigning the value 200.2 to the convertionRate variable:
    convertionRate = 200.2;

    writeln("Today's convertion rate is ", convertionRate); 
}
  • Today’s conversion rate is 200.2
  • Today’s conversion rate is conversionRate
  • An error will be thrown
  • None of these

Q4. write is short for stdout.write.

  • True
  • False

Q5. In D, the character used to find the address of a variable is

  • &
  • %
  • %s
  • Both B and C

Challenge 3: Fix the Code

Answer:

import std.stdio;

void isBetween() {
    int value = 21;
    
    write("Is between: ", (value > 10) && (value < 20));
}

Challenge 4: Find Remainder and Quotient of an Integer

Answer:

import std.stdio;

void DivideInteger() {
    int first = 7;

    int second = 3;

    int quotient = first / second;
    int remainder = first % second;

    write(first," = ",second," multiplied by " ,quotient," plus " ,remainder); 
}

Challenge 5: Find Twice and One Fifth of a Floating Point Value

Answer:

import std.stdio;

void TwiceAndOneFifth() {
    double value = 2.37;
    double twice;
    double oneFifth;

    twice = value * 2; 

    oneFifth = value / 5; 

    write("Twice = ",twice," and One Fifth = " ,oneFifth); 

}

Quiz 4: Arithmetic and Logical Operations on Data Types

Q1. int and uint have the same size.

  • True
  • False

Q2. Predict the output of following program:

import std.stdio;

void main() {
    writeln(3 ^^ 2 + 5 *2); 
}
  • 28
  • 19
  • 531441
  • None of these

Q3. In case of signed addition, which of the following functions can be used to detect overflow?

  • add
  • addu
  • adds
  • Overflow can’t be detected in this case

Q4. The number of bits used to represent a double type variable is:

  • 16
  • 32
  • 64
  • Depends on the hardware support

Q5. In order to avoid truncation problems, which data type is preferred?

  • int
  • double

Challenge 6: Print a Parallelogram Pattern

Answer:

import std.stdio;

void printParallelogram() { 
    int rows = 5;
    int cols = 8;

    for (int line = 0; line != rows; ++line) {
        for (int i = 0; i != line; ++i) {
            write(' ');
        }

        writeln("########");
    }
    
}

Quiz 5: Conditional Statements and Loops

Q1. Which statement can be used to start the next iteration of the loop right away?

  • break
  • continue
  • while
  • None of these

Q2. Which statement can be used to exit the loop right away?

  • break
  • continue
  • Both a and b

Q3. Predict the output of the following program:

import std.stdio; 

void main() {
    bool takeRest = false;
    
    if (takeRest) { 
        writeln("Finally, a break!");
    }
    
    else
    {
        writeln("Keep on learning");
    }
}
  • Finally, a break!
  • Keep on learning
  • Finally, a break! Keep on learning
  • None of these

Q4. What defines the scope of a block of code?

  • if statement
  • while loop
  • curly brackets
  • space between the two blocks of code

Q5. An if statement can be used in the scope of an else statement.

  • True
  • False

Challenge 7: Sort and Reverse Elements of an Array

Answer:

 import std.stdio; 
 import std.algorithm;

 void SortAndReverse() {
    int[] values=[ 20, 13, 4, 9, 11 ]; 

    // The counter is commonly named as 'i'
    int i;

    writeln("Sorted Elements: "); 
    sort(values);

    i = 0;
    while (i < values.length) {
        write(values[i], " ");
        ++i; 
    }
    writeln();

    writeln("Reversed Elements: "); 
    reverse(values);
    
    i = 0;
    while (i < values.length) {
        write(values[i], " ");
        ++i; 
    }
}

Challenge 8: Halve the Elements of the Array

Answer:

import std.stdio;

void Halve() {

    double[] array = [ 1, 20, 2, 30, 7, 11 ];

    double[] slice = array;    // Start with a slice that
                               // provides access to all of
                               // the elements of the array

    while (slice.length) {     // As long as there is at least
                               // one element in that slice

        if (slice[0] > 10) {   // Always use the first element
            slice[0] /= 2;     // in the expressions
        }

        slice = slice[1 .. $]; // Shorten the slice from the
                               // beginning
    }

    int i = 0;

    while(i<array.length) {
    
        write(array[i]," ");
        ++i;
    }

}

Challenge 9: Print Part of a String

Answer:

import std.stdio;
import std.string;

void PrintPart() {

    string line = "this line has five words";

    ptrdiff_t first_e = indexOf(line, 'e');

    if (first_e == -1) {
        writeln("There is no letter e in this line.");

    } else {
        ptrdiff_t last_e = lastIndexOf(line, 'e');
        writeln(line[first_e .. last_e + 1]);
    }
}

Quiz 6: Arrays, Characters and Strings

Q1. We can use the .length property of arrays to specify the length of a dynamic array.

  • True
  • False

Q2. What will be the output of the following code?


import std.stdio;
import std.algorithm;

void main() {
    int[] array; 
    array ~= [10,20];
    array ~= 40; 
    array = array.remove(0);               
    writeln(array);
}
  • [10, 20]
  • [10, 40]
  • [20, 40]
  • [10]

Q3. Modifying an element of a slice modifies the element of the actual array.

  • True
  • False

Q4. While reading string input, which function can be used to remove the trailing control character?

  • readf()
  • readln()
  • strip(readln())
  • None of these

Q5. Which character/characters can be used for string concatenation?

  • ~
  • ~=
  • +=
  • ~+

Quiz 7: File Handling in D

Q1. Standard input can be redirected from a file by using the ____ operator?

  • >
  • <
  • >>
  • <<

Q2. File struct is part of which module?

  • std.file
  • std.stdio
  • stdin
  • stdout

Q3. While reading from a file, the eof() function returns ____ when the end of a file is reached?

  • True
  • False

Q4. If a file is opened in w+ mode, the user has

  • read access
  • write access
  • read and write access
  • None of these

Q5. Assume that a by_two program multiplies a number by two, and one_tenth program divides a number by ten. Now, if the number passed to by_two program is 10, what will be the collective output of the following command be?

./by_two | ./one_tenth
  • 2
  • 4
  • 5
  • 10

Challenge 10: Fix the Code

Answer:

import std.stdio;

void DisplayAmount() {
    auto amount = 10_000_000_000;
    
    write(amount);
}

Quiz 8: Literals and enum

Q1. Integer literals can be written in which of the following way/ways?

  • Decimal system
  • Hexadecimal system
  • Binary system
  • Octal system
  • All of the above

Q2. Unless explicitly specified, the type of a floating point literal is ___ ?

  • int
  • long
  • double
  • None of these

Q3. A character literal can be specified

  • as the character itself
  • by a backslash character followed by a special character
  • by their ASCII codes
  • All of the above

Q4. A string literal with an appended d character specifies the type of the string explicitly as

  • immutable(char)[]
  • immutable(wchar)[]
  • immutable(dchar)[]
  • Both a and c

Q5. Which of the following makes the code more readable and maintainable?

  • Magic constants
  • enum

Challenge 11: Convert a Floating Point Value into a Percentage Value

Answer:

import std.stdio;

void Convert() {

    double value = 1.2345;

    writefln("%%%.2f", value);
}

Quiz 9: Formatted Output and Input

Q1. What will the output of the following program be?

import std.stdio;

void main(){
    writefln("%,s", 1000000);   
}
  • 1000000
  • 1,0,0,0,0,0,0
  • 1,000,000
  • None of these

Q2. For a floating point argument, the default value of precision after the decimal mark is:

  • 4 digits
  • 5 digits
  • 6 digits
  • 8 digits

Q3. What is the effect of + flag on a negative value?

  • Displays the value with + sign before it
  • Displays the value without the – sign
  • Has no effect
  • None of these

Q4. The format() function is part of which module?

  • std.stdio
  • std.format
  • std.string
  • None of these

Q5. While reading the data, the format specifier s specifies to

  • read the data as int
  • read the data as string
  • read according to the type of variable
  • read the data as a single character

Challenge 12: Swap Keys with Values of an Associative Array

Answer:

import std.stdio;

void Swap() {
    string[int] names = [ 1:"one", 7:"seven", 20:"twenty" ];

    int[string] values;

    foreach (key, value; names) {
        values[value] = key;
    }

    write(values["twenty"]);
}

Challenge 13: Build a Calculator Application

Answer:

    final switch (operation) {

    case "add":
        result = first + second;
        break;

    case "subtract":
        result = first - second;
        break;

    case "multiply":
        result = first * second;
        break;

    case "divide":
        result = first / second;
        break;
    }

    writeln(result);

Quiz 10: foreach Loop and switch-case Statement

Q1. foreach is simpler than for, because it increments the array indexes automatically with every iteration?

  • True
  • False

Q2. What will the output of the following program be?

import std.stdio;

void main() {
    int[] numbers = [ 45, 30,15 ];

    foreach (number; numbers) {
        number /= 3;
    }

    writefln("After dividing by 3: %s", numbers);
}
  • After dividing by 3: [15, 10, 5]
  • After dividing by 3: [45, 30, 15]
  • An error is thrown
  • None of these

Q3. When using foreach loop with associative arrays, a single name refers to the ____

  • key
  • value
  • can be either key or value
  • None of these

Q4. goto case causes the execution to continue to the ____

  • default section
  • first case statement
  • next case
  • executes the current case again

Q5. The final switch can not have a default section.

  • True
  • False

Challenge 14: displayMenu() Function

Answer:

void printMenu(string[] items, int firstNumber) {
    foreach (i, item; items) {
        writeln( i + firstNumber, ' ', item);
    }
}

Quiz 11: Functions

Q1. If a function has more than one return statement, then the value of the last return statement that gets executed determines the return value of the function of a particular call.

  • True
  • False

Q2. Functions that do not return any value to the caller have return type as ____

  • int
  • char
  • void
  • None of these

Q3. It is possible to have a function in the code that does not get executed.

  • True
  • False

Q4. In the code below

    add( 5, 10);

the values ‘5’ and ‘10’ are:

  • Function parameters
  • Function arguments

Q5. Functions help remove ____

  • code duplication
  • multiple chances for bugs to crop up
  • the need of redundant comments
  • All of the above

Quiz 12: Immutability

Q1. Like enum, we can not refer to the memory location of immutable variables.

  • True
  • False

Q2. Which keyword in D expresses the immutability concept?

  • const
  • immutable
  • Both a and b
  • None of these

Q3. In order to pass an immutable variable to a function, the parameter should be specified as:

  • immutable
  • mutable
  • const
  • None of these

Q4. .idup makes ____

  • a mutable copy of the array
  • an immutable copy of the array
  • does not make the copy, but makes original array mutable
  • does not make the copy, but makes original array immutable

Q5. In functions that do not modify values of variables, immutable/mutable are preferred.

  • True
  • False

Quiz 13: Value Types and Reference Types

Q1. Fixed-length arrays are value types.

  • True
  • False

Q2. In the code given below, both writeln() statements will produce the same output.

import std.stdio;

void main(){
    int value = 10;
    int newValue = value;
    
    writeln("value : ", value, " address: ", &value); 
    writeln("newValue: ", newValue, " address: ", &newValue);
}
  • True
  • False

Q3. Reference variables do not have identities; they are aliases of existing variables.

  • True
  • False

Q4. What will the output of the following program be?

import std.stdio;

void main() {
    int number = 4;
    byTwo(number);    
   
    writeln(number);
}
void byTwo(int value) {
    value *= 2; 
}
  • 4
  • 8
  • Compilation error
  • None of these

Q5. Is it possible to have a variable of reference type without referencing any object?

  • Yes
  • No

Challenge 15: Fix the Code

Solution:

void swap(ref int first, ref int second) { 
    const int temp = first;
    first = second;
    second = temp;
}

Quiz 14: Function Parameters

Q1. A modification made through the referenced object modifies the original object as well.

  • True
  • False

Q2. If a parameter is specified as in, any changes made to it in the function will reflect in the caller function as well.

  • True
  • False

Q3. When a value is passed to a function through an out specified parameter, the function receives ____.

  • the exact value
  • the ‘.init’ value
  • the exact value but does not allow it to be used in the function
  • None of these

Q4. rvalues ____.

  • don’t have memory addresses
  • cannot be assigned new values
  • cannot be passed to functions by reference
  • All of the above

Q5. It is possible to determine whether the parameter is an lvalue or an rvalue by using _traits(isRef) with static if.

  • True
  • False

Quiz 15: Program Environment

Q1. void main() function that terminates successfully returns ___ as its return value.

  • 1
  • 0
  • null
  • None of these

Q2. The ___ module is designed to help with parsing the command-line options of the program.

  • std.stderr
  • std.stdin
  • std.getopt
  • std.stdout

Q3. std.process.environment provides access to the environment variables through the associative array syntax.

  • True
  • False

Q4. The function executeshell is part of which module?

  • std.getopt
  • std.process
  • std.stdio
  • None of these

Q5. std.process helps with accessing environment variables and starting other programs.

  • True
  • False

Quiz 16: Exceptions and scope

Q1. Catch blocks ____.

  • specify that an error occurred in the program
  • contain a message that can explain the error further
  • contain information about the type of error
  • Both A and B

Q2. Expressions that are written in the finally block get executed regardless of whether an exception is thrown or not.

  • True
  • False

Q3. Which one of the following is a property of exceptions?

  • .file
  • .line
  • .info
  • All of the above

Q4. We can validate the data before using it in the program with the help of ___ function.

  • assert()
  • exists()
  • check()
  • None of these

Q5.We should use assert to validate program logic and implementation.

  • True
  • False

Quiz 17: assert and enforce Statements

Q1. Which of the following is the correct way to use an assert statement?

  • assert(logical_expression);
  • assert(logical_expression, message);
  • Both A and B
  • None of these

Q2. static assert is applied at compile time.

  • True
  • False

Q3. Which compiler switch can be used to ignore assert checks in the program?

  • -de
  • -unittest
  • -release
  • -w

Q4. If the user enters a wrong input, then which of the following statement is preferable to deal with the situation?

  • assert
  • enforce

Quiz 18: Unit Testing

Q1. When a program that is built by the ‐unittest switch is started, its unit test blocks are executed first. Only if all of the unit tests pass does the program execution continue with main().

If a program is built by the -unittest switch, its unit test blocks are executed _____.

  • before the execution of the main() function
  • after the execution of the main() function
  • when unittest blocks are called from the main() function
  • at the end of the program

Q2. Which of the following functions from the std.exception module can help with testing for exceptions?

  • assert()
  • assertNotThrown()
  • assertThrown()
  • Both B and C

Q3. Is it safe to assume that a program having unittests will not have any bugs?

  • Yes
  • No

Q4. If a bug is caught in the program, what should be done first according to TDD?

  • Fix the bug in the program
  • Write a unit test to reproduce the bug
  • Ignore the bug if it is not much significant

Quiz 19: Contract Programming

Q1. If one of the assert check fails in the in block of a function, will the function still proceed with the execution of statements in the do block?

  • Yes
  • No

Q2. Which of the following tools can be used to check program correctness?

  • contracts
  • unittests
  • Both A and B
  • None of these

Q3. If the function has to throw an exception because of invalid parameter values, then which of the following check statements is most suitable?

  • assert
  • in
  • enforce
  • out

Q4. When an in or out block is used, the actual body of the function must be specified as a do block.

  • True
  • False
Conclusion:

I hope this Programming in D: The Ultimate Guide for Software Engineers Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.