Namespaces Questions with Answers

8 practice questions on Namespaces (C#) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following statements is correct about a namespace used in C#.NET?

  1. Nested namespaces are not allowed.
  2. Importing outer namespace imports inner namespace.
  3. Nested namespaces are allowed.
  4. If nested, the namespaces cannot be split across files.

Answer: C. Nested namespaces are allowed.

Explanation

Namespaces can be nested where you can define one namespace inside another namespace.

Example of the nested namespaces
namespace namespace_name1
{
   // code declarations
      namespace namespace_name2
   {
     // code declarations
   }
}

Step-by-step solution

  1. A namespace can be declared inside another namespace; this is nesting.
  2. The example shows namespace_name2 declared inside namespace_name1.
  3. So nested namespaces are allowed: option C.

Why the other options are wrong

  • A. Nesting is allowed, so 'not allowed' is false.
  • B. Importing the outer namespace does not import the inner one; the inner namespace needs its own using.
  • D. A namespace, nested or not, can be split across several files.

Tip: Nesting does not import: write the outer.inner name or add a using for the inner namespace.

Question 2Medium

Which is correct statement about the namespaces in C#.NET?

  1. Nesting of namespaces is permitted, provided all the inner namespaces are declared in the same file.
  2. A namespace cannot be tested
  3. There is no limit on the number of levels while nesting namespaces.
  4. All of the mentioned.

Answer: C. There is no limit on the number of levels while nesting namespaces.

Explanation

Your entire application resides under its own special namespace, often named after your application or project name. Sometimes, companies with an entire product series decide to use nested namespaces though, where the "root" namespace can share the name of the company, and the nested namespaces the respective project names. This can be especially convenient, if you're a developer who has made library with some usual functionality that can be shared across programs.
If both the library and your program shared a parent namespace, that one would then not have to be explicitly declared with the using keyword, and still not have to be completely typed out.
If your code was open for others to use, third party developers that may use your code would additionally then see that the same company had developed the library and the program.
The developer of the library and program would finally also separate all the named information in their product source codes, for fewer headaches especially, if common names are used.To make your application reside in a nested namespace, you can show this in ways.

 namespace CodeWorks
 {
     namespace MyApplication
     {
         // Do stuff
     }
 }

Step-by-step solution

  1. C# allows namespaces to be nested to any depth. The language sets no fixed limit on the number of levels.
  2. Inner namespaces do not need to be in the same file, so the first option is false.
  3. Namespaces only group types, so the types inside them are what get tested. The second option is false.
  4. Because the third option is true, 'All of the mentioned' is false.

Why the other options are wrong

  • A. Nested namespaces can be declared in other files. The same-file rule is not a real limit.
  • B. Namespaces only group types. The types inside them are the testable parts.
  • D. The third option is true, so 'All of the mentioned' cannot be right.

Tip: Namespaces are for organising names. They do not add behaviour of their own.

Question 3Medium

Which of the following is absolutely necessary to use a class Point present in namespace Graph stored in a library?

  1. Use the fully qualified name of the Point class
  2. Use a using statement before using the Point class
  3. Add a reference to the library before using the Point class
  4. Copy the library's source code into the project

Answer: C. Add a reference to the library before using the Point class

Explanation

Without a reference to the library assembly, the compiler cannot find the class at all. A using directive or fully qualified name is just a naming choice.

Step-by-step solution

  1. To use a class from a library, you must first add a reference to that library.
  2. The compiler needs to know where to find the class definition.
  3. Adding a reference makes the class available for use in the project.
  4. A using directive or fully qualified name does not replace the need for a reference.
  5. Therefore, adding a reference to the library is absolutely necessary.

Why the other options are wrong

  • A. Fully qualified name avoids naming conflicts but does not replace the need for a library reference.
  • B. A using statement helps with naming but does not make the class available if the library is not referenced.
  • D. Copying source code is unnecessary if the library is already referenced.

Tip: Always add a reference to any external library before using its classes.

Question 4Medium

Which is the not a namespace in the .NETFramework Class Liberary?

  1. System.Process
  2. System.Security.
  3. System.Threading.
  4. System.xml.

Answer: A. System.Process

Explanation

System.Process is not a .NET namespace; the Process class is in System.Diagnostics. System.Security, System.Threading and System.Xml are real .NET namespaces.

Step-by-step solution

  1. System.Security is a namespace for security classes.
  2. System.Threading is a namespace for threads.
  3. System.Xml (written System.xml in the option) is the namespace for XML classes.
  4. System.Process is not a namespace; Process is a class in System.Diagnostics, so the answer is option A.

Why the other options are wrong

  • B. System.Security is a real namespace.
  • C. System.Threading is a real namespace.
  • D. System.xml is the XML namespace (correct casing System.Xml), so it is a real namespace.

Tip: Check each dotted name: a class such as Process is not a namespace even when it sounds like one.

Question 5Medium

Which of the Namespaces are used in c#.net?

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Windows.Forms;
  4. All of the above are used

Answer: D. All of the above are used

Explanation

All three are used in C# code. using System; gives the basic types, using System.Collections.Generic; gives generic collections such as List and Dictionary, and using System.Windows.Forms; is used for Windows Forms applications. So all of the above is correct.

Step-by-step solution

  1. Using System; is a namespace used for fundamental classes.
  2. Using System.Collections.Generic; is a namespace for generic collections.
  3. Using System.Windows.Forms; is a namespace for Windows forms applications.
  4. All three namespaces are valid and used in c#.net.
  5. Final answer: All of the above are used.

Why the other options are wrong

  • A. Only one namespace is mentioned, but others are also used.
  • B. Only one namespace is mentioned, but others are also used.
  • C. Only one namespace is mentioned, but others are also used.

Tip: Remember that multiple namespaces can be used together in a single project.

Question 6Medium

Which of the followings are NOT a .NET namespace?
I. System.Web
II. System.Process
III. System.Data.
IV. System.Drawing2D.
V. System.Drawing3D

  1. I, III
  2. II, IV, V.
  3. III, V.
  4. I, II, III

Answer: B. II, IV, V.

Explanation

System.Process and System.Drawing3D are not .NET namespaces, and the name System.Drawing2D is not a namespace either (the real one is System.Drawing.Drawing2D). System.Web and System.Data are real namespaces, so II, IV and V are the ones that are not.

Step-by-step solution

  1. System.Web exists as a .NET namespace for web functionality.
  2. System.Data exists as a .NET namespace for data access.
  3. System.Process, System.Drawing2D, and System.Drawing3D are not defined .NET namespaces.
  4. II, IV, V.

Why the other options are wrong

  • A. System.Web and System.Data both exist in .NET, so neither belongs in the not-namespace list.
  • C. System.Data is a real .NET namespace, so listing III as not a namespace is wrong.
  • D. System.Web is a real .NET namespace, so listing I as not a namespace is wrong.

Tip: Verify the exact namespace spelling; many have extra segments such as Drawing.Drawing2D.

Question 7Medium

Which of the following is NOT a namespacein the .NET Framework Class Library?

  1. System.Process
  2. System.Security
  3. System.Threading
  4. System.Drawing

Answer: A. System.Process

Explanation

System.Process is not a .NET namespace; the Process class is in System.Diagnostics. System.Security, System.Threading and System.Drawing are real namespaces.

Step-by-step solution

  1. System.Security, System.Threading and System.Drawing are all namespaces in the .NET Framework class library.
  2. System.Process is not a namespace; Process is a class in the System.Diagnostics namespace.
  3. So the answer is option A, System.Process.

Why the other options are wrong

  • B. System.Security is a real namespace.
  • C. System.Threading is a real namespace.
  • D. System.Drawing is a real namespace.

Tip: Namespaces are dotted groups such as System.IO; a class like Process sits inside a namespace (System.Diagnostics).

Question 8Medium

Which of the following CANNOT belong to a C#.NET Namespace?

  1. Class
  2. Struct
  3. Enum
  4. Data

Answer: D. Data

Explanation

C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces.

Step-by-step solution

  1. A namespace can contain classes, structs, enums, interfaces, delegates, and other namespaces.
  2. Class, Struct, and Enum are valid types that can be placed inside a namespace.
  3. Data is not a defined C# type category.
  4. Hence Data cannot be included in a namespace.
  5. Data.

Why the other options are wrong

  • A. Classes can be declared inside a namespace, so Class is a valid member type of a namespace.
  • B. Structs can be declared inside a namespace, so Struct is a valid member type here.
  • C. Enums can be declared inside a namespace, so Enum is a valid member type of a namespace.

Tip: Only recognized C# type categories can be members of a namespace; arbitrary names like Data cannot.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics