Experienced in D Programming Language? D Programming Language is a object oriented and system programming language created in 2001. D's modeling, productivity, readability, and other features make it a good fit for collaborative software development. It's a practical language for practical programmers who need to get the job done quickly, reliably, and leave behind maintainable, easy to understand code. The code similar to C and C++. Good hands on knowledge on the D Programming Language will put you ahead in interview. Every where, we can find job opportunities for this position. Wisdomjobs has interview questions which are exclusively designed for employees to assist them in clearing interviews. D Programming Language interview questions are useful for employees who are good at D Programming Language.
Question 1. Why Doesn't The Case Range Statement Use The Case X..y: Syntax?
Answer :
The usages of .. would then be:
Case (1) has a VERY DIFFERENT meaning from (2) and (3). (1) is inclusive of Y, and (2) and (3) are exclusive of Y. Having a very different meaning means it should have a distinctly different syntax.
Question 2. What Guarantees Are Shared Supposed To Provide?
Answer :
Shared means that multiple threads can access the data. The guarantee is that if it is not shared, and not immutable, that only the current thread can see it.
Question 3. What Does Shared Have To Do With Synchronization?
Answer :
Only shared data can be synchronized. It makes no sense to synchronize thread local data.
Question 4. What Are The Semantics Of Casting From Unshared To Shared?
Answer :
Make sure there are no other unshared references to that same data.
Question 5. What Are The Semantics Of Casting From Shared To Unshared?
Answer :
Make sure there are no other shared references to that same data.
Question 6. Why Does A Large Static Array Bloat My Executable File Size?
Answer :
Given the declaration:
char[1024 * 1024] arr;
the executable size increases by a megabyte in size. In C, this would not as arr would be stored in the BSS segment. In D, arr is not stored in the BSS segment because:
The following will be placed in BSS:
__gshared byte[1024 * 1024] arr;
as bytes are 0 initialized and __gshared puts it in the global data.
There are similiar issues for float, double, and real static arrays. They are initialized to NaN (Not A Number) values, not 0.
The easiest way to deal with this issue is to allocate the array dynamically at run time rather than statically allocate it.
Answer :
The original name was the Mars Programming Language. But my friends kept calling it D, and I found myself starting to call it D. The idea of D being a successor to C goes back at least as far as 1988, as in this thread.
Question 8. Could You Change The Name? D Is Kind Of Hard To Search For On Search Engines?
Answer :
No. We understand it can be frustrating but it's far too late for a name change at this point. We recommend using "dlang", "d programming", "d language", or "d programming language" in your search terms. Doing so should yield substantially better search results.
Most publicly available D code has "// Written in the D programming language" as its first comment.
Question 9. Is There A Linux Port Of D?
Answer :
Yes, the D compiler includes a linux version.
Question 10. Is There A Gnu Version Of D?
Answer :
Yes, gdc - the D frontend with GCC.
Question 11. How Do I Write My Own D Compiler For Cpu X?
Answer :
Burton Radons has written a back end. You can use as a guide.
Question 12. Where Can I Get A Gui Library For D?
Answer :
Since D can call C functions, any GUI library with a C interface is accessible from D. Various D GUI libraries and ports can be found at the D wiki.
Question 13. Where Can I Get An Ide For D?
Answer :
Lists of editors and IDEs that support D can be found on the D wiki.
Question 14. Why Is Printf In D?
Answer :
printf is not part of D, it is part of C's standard runtime library which is accessible from D. D's standard runtime library has std.stdio.writefln, which is as powerful as printf but is much easier to use.
Question 15. Is D Open Source?
Answer :
The front end for the dmd D compiler is open source. The back end for dmd is licensed from Symantec, and is not compatible with open-source licenses such as the GPL. Nonetheless, the complete source comes with the compiler, and all development takes place publicly on github. Compilers using the DMD front end and the GCC and LLVM open source backends are also available. The runtime library is completely open source using the Boost License 1.0. The gdc and ldcD compilers are completely open sourced.
Question 16. Why Does The Standard Library Use The Boost License? Why Not Public Domain?
Answer :
Although most jurisdictions use the concept of Public Domain, some (eg, Japan) do not. The Boost License avoids this problem. It was chosen because, unlike almost all other open source licenses, it does not demand that the license text be included on distributions in binary form.
Question 17. Why No Fall Through On Switch Statements?
Answer :
Many people have asked for a requirement that there be a break between cases in a switch statement, that C's behavior of silently falling through is the cause of many bugs.
In D2, implicit fall through is disallowed. You have to add a goto case; statement to explicitly state the intention of falling through.
There was further request that the break statement be made implicit. The reason D doesn't change this is for the same reason that integral promotion rules and operator precedence rules were kept the same - to make code that looks the same as in C operate the same. If it had subtly different semantics, it will cause frustratingly subtle bugs.
Question 18. Why Should I Use D Instead Of Java?
Answer :
D is distinct from Java in purpose, philosophy and reality. See this comparison.
Java is designed to be write once, run everywhere. D is designed for writing efficient native system apps. Although D and Java share the notion that garbage collection is good and multiple inheritance is bad, their different design goals mean the languages have very different feels.
Question 19. Doesn't C++ Support Strings, Etc. With Stl?
Answer :
In the C++ standard library are mechanisms for doing strings, dynamic arrays, associative arrays, and bounds-checked arrays.
Sure, all this stuff can be done with libraries, following certain coding disciplines, etc. But object oriented programming can also be done in C (it's been done). Isn't it incongruous that something like strings, supported by the simplest BASIC interpreter, requires a very large and complicated infrastructure to support? Just the implementation of a string type in STL is over two thousand lines of code, using every advanced feature of templates. How much confidence can you have that this is all working correctly, how do you fix it if it is not, what do you do with the notoriously inscrutable error messages when there's an error using it, how can you be sure you are using it correctly (so there are no memory leaks, etc.)?
D's implementation of strings is simple and straightforward. There's little doubt about how to use it, no worries about memory leaks, error messages are to the point, and it isn't hard to see if it is working as expected or not.
Question 20. Can't Garbage Collection Be Done In C++ With An Add-on Library?
Answer :
Yes, I use one myself. It isn't part of the language, though, and requires some subverting of the language to make it work. Using gc with C++ isn't for the standard or casual C++ programmer. Building it into the language, like in D, makes it practical for everyday programming chores.
GC isn't that hard to implement, either, unless you're building one of the more advanced ones. But a more advanced one is like building a better optimizer - the language still works 100% correctly even with a simple, basic one. The programming community is better served by multiple implementations competing on quality of code generated rather than by which corners of the spec are implemented at all.
Question 21. Can't Unit Testing Be Done In C++ With An Add-on Library?
Answer :
Sure. Try one out and then compare it with how D does it. It'll be quickly obvious what an improvement building it into the language is.
Question 22. Why Have An Asm Statement In A Portable Language?
Answer :
An asm statement allows assembly code to be inserted directly into a D function. Assembler code will obviously be inherently non-portable. D is intended, however, to be a useful language for developing systems apps. Systems apps almost invariably wind up with system dependent code in them anyway, inline asm isn't much different. Inline asm will be useful for things like accessing special CPU instructions, accessing flag bits, special computational situations, and super optimizing a piece of code.
Before the C compiler had an inline assembler, I used external assemblers. There was constant grief because many, many different versions of the assembler were out there, the vendors kept changing the syntax of the assemblers, there were many different bugs in different versions, and even the command line syntax kept changing. What it all meant was that users could not reliably rebuild any code that needed assembler. An inline assembler provided reliability and consistency.
Question 23. What Is The Point Of 80 Bit Reals?
Answer :
More precision enables more accurate floating point computations to be done, especially when adding together large numbers of small real numbers. Prof. Kahan, who designed the Intel floating point unit, has an eloquent paper on the subject.
Question 24. How Do I Do Anonymous Struct/unions In D?
Answer :
import std.stdio;
struct Foo
{
union { int a; int b; }
struct { int c; int d; }
}
void main()
{
writefln(
"Foo.sizeof = %d, a.offset = %d, b.offset = %d, c.offset = %d, d.offset = %d",
Foo.sizeof,
Foo.a.offsetof,
Foo.b.offsetof,
Foo.c.offsetof,
Foo.d.offsetof);
}
Question 25. How Do I Get Printf() To Work With Strings?
Answer :
In C, the normal way to printf a string is to use the %s format:
char s[8];
strcpy(s, "foo");
printf("string = '%s'n", s);
Attempting this in D, as in:
char[] s;
s = "foo";
printf("string = '$(B %s)'n", s);
usually results in garbage being printed, or an access violation. The cause is that in C, strings are terminated by a 0 character. The %s format prints until a 0 is encountered. In D, strings are not 0 terminated, the size is determined by a separate length value. So, strings are printf'd using the %.*s format:
char[] s;
s = "foo";
printf("string = '$(B %.*s)'n", s);
which will behave as expected. Remember, though, that printf's %.*s will print until the length is reached or a 0 is encountered, so D strings with embedded 0's will only print up to the first 0.
Of course, the easier solution is just use std.stdio.writefln which works correctly with D strings.
Question 26. Why Are Floating Point Values Default Initialized To Nan Rather Than 0?
Answer :
A floating point value, if no explicit initializer is given, is initialized to NaN (Not A Number):
double d; // d is set to double.nan
NaNs have the interesting property in that whenever a NaN is used as an operand in a computation, the result is a NaN. Therefore, NaNs will propagate and appear in the output whenever a computation made use of one. This implies that a NaN appearing in the output is an unambiguous indication of the use of an uninitialized variable.
If 0.0 was used as the default initializer for floating point values, its effect could easily be unnoticed in the output, and so if the default initializer was unintended, the bug may go unrecognized.
The default initializer value is not meant to be a useful value, it is meant to expose bugs. Nan fills that role well.
But surely the compiler can detect and issue an error message for variables used that are not initialized? Most of the time, it can, but not always, and what it can do is dependent on the sophistication of the compiler's internal data flow analysis. Hence, relying on such is unportable and unreliable.
Because of the way CPUs are designed, there is no NaN value for integers, so D uses 0 instead. It doesn't have the advantages of error detection that NaN has, but at least errors resulting from unintended default initializations will be consistent and therefore more debuggable.
Question 27. Why Is Overloading Of The Assignment Operator Not Supported?
Answer :
Overloading of the assignment operator for structs is supported in D 2.0.
Question 28. The ‘~’ Is Not On My Keyboard?
Answer :
On PC keyboards, hold down the [Alt] key and press the 1, 2, and 6 keys in sequence on the numeric pad. That will generate a ‘~’ character.
Question 29. Can I Link In C Object Files Created With Another Compiler?
Answer :
DMD produces OMF (Microsoft Object Module Format) object files while other compilers such as VC++ produce COFF object files. DMD's output is designed to work with DMC, the Digital Mars C compiler, which also produces object files in OMF format.
The OMF format that DMD uses is a Microsoft defined format based on an earlier Intel designed one. Microsoft at one point decided to abandon it in favor of a Microsoft defined variant on COFF.
Using the same object format doesn't mean that any C library in that format will successfully link and run. There is a lot more compatibility required - such as calling conventions, name mangling, compiler helper functions, and hidden assumptions about the way things work. If DMD produced Microsoft COFF output files, there is still little chance that they would work successfully with object files designed and tested for use with VC. There were a lot of problems with this back when Microsoft's compilers did generate OMF.
Having a different object file format makes it helpful in identifying library files that were not tested to work with DMD. If they are not, weird problems would result even if they successfully managed to link them together. It really takes an expert to get a binary built with a compiler from one vendor to work with the output of another vendor's compiler.
That said, the linux version of DMD produces object files in the ELF format which is standard on linux, and it is specifically designed to work with the standard linux C compiler, gcc.
Question 30. Why Not Support Regular Expression Literals With The /foo/g Syntax?
Answer :
There are two reasons:
Question 31. Why Aren't All Digital Mars Programs Translated To D?
Answer :
There is little benefit to translating a complex, debugged, working application from one language to another. But new Digital Mars apps are implemented in D.
Question 32. When Should I Use A Foreach Loop Rather Than A For?
Answer :
By using foreach, you are letting the compiler decide on the optimization rather than worrying about it yourself. For example - are pointers or indices better? Should I cache the termination condition or not? Should I rotate the loop or not? The answers to these questions are not easy, and can vary from machine to machine. Like register assignment, let the compiler do the optimization.
for (int i = 0; i < foo.length; i++)
or:
for (int i = 0; i < foo.length; ++i)
or:
for (T* p = &foo[0]; p < &foo[length]; p++)
or:
T* pend = &foo[length];
for (T* p = &foo[0]; p < pend; ++p)
or:
T* pend = &foo[length];
T* p = &foo[0];
if (p < pend)
{
do
{
...
} while (++p < pend);
}
and, of course, should I use size_t or int?
for (size_t i = 0; i < foo.length; i++)
Let the compiler pick!
foreach (v; foo)
...
Note that we don't even need to know what the type T needs to be, thus avoiding bugs when T changes. I don't even have to know if foo is an array, or an associative array, or a struct, or a collection class. This will also avoid the common fencepost bug:
for (int i = 0; i <= foo.length; i++)
And it also avoids the need to manually create a temporary if foo is a function call.
The only reason to use a for loop is if your loop does not fit in the conventional form, like if you want to change the termination condition on the fly.
Question 33. Why Doesn't D Have An Interface To C++ As Well As C?
Answer :
Here are some reasons why it isn't a full interface:
Attempting to have D interface with C++ is nearly as complicated as writing a C++ compiler, which would destroy the goal of having D be a reasonably easy language to implement. For people with an existing C++ code base that they must work with, they are stuck with C++ (they can't move it to any other language, either).
There are many issues that would have to be resolved in order for D code to call some arbitrary C++ code that is presumed to be unmodifiable. This list certainly isn't complete, it's just to show the scope of the difficulties involved.
The bottom line is the language features affect the design of the code. C++ designs just don't fit with D. Even if you could find a way to automatically adapt between the two, the result will be about as enticing as the left side of a honda welded to the right side of a camaro.
Question 34. Why Doesn't D Use Reference Counting For Garbage Collection?
Answer :
Reference counting has its advantages, but some severe disadvantages:
The proposed C++ shared_ptr<>, which implements ref counting, suffers from all these faults. I haven't seen a heads up benchmark of shared_ptr<> vs mark/sweep, but I wouldn't be surprised if shared_ptr<> turned out to be a significant loser in terms of both performance and memory consumption.
That said, D may in the future optionally support some form of ref counting, as rc is better for managing scarce resources like file handles. Furthermore, if ref counting is a must, Phobos has the std.typecons.RefCounted type which implements it as a library, similar to C++'s shared_ptr<>.
Question 35. Isn't Garbage Collection Slow And Non-deterministic?
Answer :
Yes, but all dynamic memory management is slow and non-deterministic, including malloc/free. If you talk to the people who actually do real time software, they don't use malloc/free precisely because they are not deterministic. They preallocate all data. However, the use of GC instead of malloc enables advanced language constructs (especially, more powerful array syntax), which greatly reduce the number of memory allocations which need to be made. This can mean that GC is actually faster than explict management.
Question 36. Can't A Sufficiently Smart Compiler Figure Out That A Function Is Pure Automatically?
Answer :
The compiler infers purity (and safety, and nothrow) for delegate and function literals. It doesn't do this for normal functions for several reasons:
Question 37. Why Allow Cast(float) If It Isn't Supposed To Work?
Answer :
The floating point rules are such that transforming cast(real)cast(float) to cast(real) is a valid transformation. This is because the floating point rules are written with the following principle in mind:
An algorithm is invalid if it breaks if the floating point precision is increased. Floating point precision is always a minimum, not a maximum.
Programs that legitimately depended on maximum precision are:
Programs that rely on a maximum accuracy need to be rethought and reengineered.
Question 38. Why Can't Nested Functions Be Forward Referenced?
Answer :
Declarations within a function are different from declarations at module scope. Within a function, initializers for variable declarations are guaranteed to run in sequential order. Allowing arbitrary forward references to nested functions would break this, because nested functions can reference any variables declared above them.
int first() { return second(); }
int x = first(); // x depends on y, which hasn't been declared yet.
int y = x + 1;
int second() { return y; }
But, forward references of nested functions are sometimes required (eg, for mutually recursive nested functions). The most general solution is to declare a local, nested struct. Any member functions (and variables!) of that struct have non-sequential semantics, so they can forward reference each other.
Question 39. Why Does D Have Const?
Answer :
People often express frustration with const and immutable in D 2.0 and wonder if it is worth it.
Of course, for writing single-threaded one man programs of fairly modest size, const is not particularly useful. And in D const can be effectively ignored by just not using it, or by using D 1.0. The only place const is imposed is with the immutable string type.
Question 40. What Principles Drove The D Const Design?
Answer :
Question 41. What Is Transitive Const?
Answer :
Transitive const means that once const is applied to a type, it applies recursively to every sub-component of that type. Hence:
const(int*)** p;
p += 1; // ok, p is mutable
*p += 1; // ok, *p is mutable
**p += 1; // error, **p is const
***p += 1; // error, ***p is const
With transitivity, there is no way to have a const pointer to mutable int.
C++ const is not transitive.
Question 42. What Is Head Const?
Answer :
Head const is where the const applies only to the component of the type adjacent to the const.
For example:
headconst(int**) p;
would be read as p being a: const pointer to mutable pointer to mutable int. D does not have head const (the headconst is there just for illustrative purposes), but C++ const is a head const system.
Question 43. What Is Tail Const?
Answer :
Tail const is the complement of head const - everything reachable from the const type is also const except for the top level.
For example:
tailconst(int**) p;
would be read as p being a: mutable pointer to const pointer to const int. Head const combined with tail const yields transitive const. D doesn't have tailconst (the keyword is there just for illustrative purposes) as a distinct type constructor.
Question 44. What Is Logical Const?
Answer :
Logical const refers to data that appears to be constant to an observer, but is not actually const. An example would be an object that does lazy evaluation:
struct Foo
{
mutable int len;
mutable bool len_done;
const char* str;
int length()
{
if (!len_done)
{
len = strlen(str);
len_done = true;
}
return len;
}
this(char* str) { this.str = str; }
}
const Foo f = Foo("hello");
bar(f.length);
The example evaluates f.len only if it is needed. Foo is logically const, because to the observer of the object its return values never change after construction. The mutable qualifier says that even if an instance of Foo is const, those fields can still change. While C++ supports the notion of logical const, D does not, and D does not have a mutable qualifier.
The problem with logical const is that const is no longer transitive. Not being transitive means there is the potential for threading race conditions, and there is no way to determine if an opaque const type has mutable members or not.
Question 45. Why Not Use Readonly To Mean Read Only View?
Answer :
Readonly has a well established meaning in software to mean ROM, or Read Only Memory that can never be changed. For computers with hardware protection for memory pages, readonly also means that the memory contents cannot be altered. Using readonly in D to mean a read only view of memory that could be altered by another alias or thread runs counter to this.
Question 46. How Does Const Differ In C++?
Answer :
C++ has a const system that is closer to D's than any other language, but it still has huge differences:
Question 47. Why Aren't Function Parameters Const By Default?
Answer :
Since most (nearly all?) function parameters will not be modified, it would seem to make sense to make them all const by default, and one would have to specifically mark as mutable those that would be changed. The problems with this are:
1. It would be a huge break from past D practice, and practice in C, C++, Java, C#, etc.
2. It would require a new keyword, say mutable.
3. And worst, it would make declarations inconsistent:
4. void foo(int* p)
5. {
6. int* q;
7. ...
8. }
p points to const, and q points to mutable. This kind of inconsistency leads to all sorts of mistakes. It also makes it very hard to write generic code that deals with types.
Using in can mitigate the ugliness of having to annotate with const:
void str_replace(in char[] haystack, in char[] needle);
Question 48. Are Static Class Members Covered By Transitive Const?
Answer :
A static class member is part of the global state of a program, not part of the state of an object. Thus, a class having a mutable static member does not violate the transitive constness of an object of that class.
Question 49. What Is Immutable Good For?
Answer :
Immutable data, once initialized, is never changed. This has many uses:
const acts as a bridge between the mutable and immutable worlds, so a single function can be used to accept both types of arguments.
D Programming Language Related Tutorials |
|
---|---|
Teradata Tutorial | C++ Tutorial |
C Tutorial | SAS Programming Tutorial |
Android Tutorial | Java Tutorial |
D Programming Language Related Practice Tests |
|
---|---|
Teradata Practice Tests | C++ Practice Tests |
C Practice Tests | Oracle Practice Tests |
SAS Programming Practice Tests | Android Practice Tests |
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.