天天看點

c# 命名空間命名規範_C#命名空間能力問題和解答 套裝4

c# 命名空間命名規範

1) Can we create more than one namespace in a single source code file?

  1. Yes
  2. No

Answer & Explanation

Correct answer: 1

Yes

Yes, we can create multiple namespaces in a single source code file.

1)我們可以在一個源代碼檔案中建立多個名稱空間嗎?

  1. 沒有

答案與解釋

正确答案:1

是的,我們可以在一個源代碼檔案中建立多個名稱空間。

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();
        }
    }
}
           
  1. Syntax error
  2. Hello
  3. Linker error
  4. Runtime exception

Answer & Explanation

Correct answer: 2

Hello

The above code will print

"Hello"

on the console screen.

2)給定代碼段的正确輸出是什麼?

  1. 文法錯誤
  2. 你好
  3. 連結器錯誤
  4. 運作時異常

答案與解釋

正确答案:2 你好

上面的代碼将在控制台螢幕上列印

“ Hello”

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();
        }
    }
}
           
  1. Syntax error
  2. Hello
  3. Linker error
  4. Runtime exception

Answer & Explanation

Correct answer: 1

Syntax error

We cannot use a modifier with a namespace.

3)給定代碼段的正确輸出是什麼?

  1. 文法錯誤
  2. 你好
  3. 連結器錯誤
  4. 運作時異常

答案與解釋

正确答案:1 文法錯誤

我們不能将修飾符與名稱空間一起使用。

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();
        }
    }
}
           
  1. Syntax error
  2. Hello
  3. Linker error
  4. Runtime exception

Answer & Explanation

Correct answer: 1

Syntax error

We cannot use a class without specifying the namespace name.

The output will be,

4)給定代碼段的正确輸出是什麼?

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 ( ) ;
        }
    }
}
           
  1. 文法錯誤
  2. 你好
  3. 連結器錯誤
  4. 運作時異常

答案與解釋

正确答案:1 文法錯誤

如果不指定名稱空間名稱,則不能使用類。

輸出将是

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();
        }
    }
}
           
  1. Syntax error
  2. Hello
  3. Linker error
  4. Runtime exception

Answer & Explanation

Correct answer: 1

Syntax error

We cannot import inner namespace like this.

The output will be,

5)給定代碼段的正确輸出是什麼?

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 ( ) ;
        }
    }
}
           
  1. 文法錯誤
  2. 你好
  3. 連結器錯誤
  4. 運作時異常

答案與解釋

正确答案:1 文法錯誤

我們不能像這樣導入内部名稱空間。

輸出将是

◀ C# Namespace Aptitude | Set 3 C# Namespace Aptitude | Set 5 ▶ #C#命名空間能力| 設定3 C#命名空間能力| 設定5▶

翻譯自: https://www.includehelp.com/dot-net/csharp-namespace-aptitude-questions-and-answers-4.aspx

c# 命名空間命名規範