Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Namespace Aptitude Questions and Answers | Set 4
C# Namespace Aptitude Questions | Set 4: This section contains aptitude questions and answers on C# Namespace.
Submitted by Nidhi, on April 08, 2020
1) Can we create more than one namespace in a single source code file?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create multiple namespaces in a single source code file.
2) What is the correct output of the given code snippet?
using System;
namespace my_namespace1
{
class sample
{
public static void sayHello()
{
Console.WriteLine("Hello");
}
}
}
namespace my_namespace2
{
class program
{
static void Main(string[] args)
{
my_namespace1.sample.sayHello();
}
}
}
- Syntax error
- Hello
- Linker error
- Runtime exception
Correct answer: 2
Hello
The above code will print "Hello" on the console screen.
3) What is the correct output of the given code snippet?
using System;
private namespace my_namespace1
{
class sample
{
public static void sayHello()
{
Console.WriteLine("Hello");
}
}
}
namespace my_namespace2
{
class program
{
static void Main(string[] args)
{
my_namespace1.sample.sayHello();
}
}
}
- Syntax error
- Hello
- Linker error
- Runtime exception
Correct answer: 1
Syntax error
We cannot use a modifier with a namespace.
4) What is the correct output of the given code snippet?
using System;
namespace my_namespace2
{
namespace my_namespace1
{
class sample
{
public static void sayHello()
{
Console.WriteLine("Hello");
}
}
}
class program
{
static void Main(string[] args)
{
sample.sayHello();
}
}
}
- Syntax error
- Hello
- Linker error
- Runtime exception
Correct answer: 1
Syntax error
We cannot use a class without specifying the namespace name.
The output will be,
The name `sample' does not exist in the current context
5) What is the correct output of the given code snippet?
using System;
using my_namespace1;
namespace my_namespace2
{
namespace my_namespace1
{
class sample
{
public static void sayHello()
{
Console.WriteLine("Hello");
}
}
}
class program
{
static void Main(string[] args)
{
sample.sayHello();
}
}
}
- Syntax error
- Hello
- Linker error
- Runtime exception
Correct answer: 1
Syntax error
We cannot import inner namespace like this.
The output will be,
The type or namespace name `my_namespace1' could not be found. Are you missing an assembly reference?