C# Questions Answers-C# structure (PartII)


C# structure (PartII)

1. What would be printed from the following C# program?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
  {
    struct student //define structure student
     {
       //variables
        public int id;
        public string name;

      }

   static void Main(string[] args)
   {

     student st; //structure object
     st.id = 100; //assign 100 to student id
     st.name = "Sok";//assign Sok to sudent name
     //output student information
     printinfo(st.id);
     printinfo(st.name);

     Console.ReadLine();

    }

   static void printinfo(int id)
   {
     Console.WriteLine("{0}", id);
    }
   static void printinfo(string name)
   {
     Console.WriteLine("{0}", name);
    }

  }
}
 
Check answer 2. What would be printed from the following C# program?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
  {


    struct Point
   {
     public int x;
     public int y;
    }

    static void Main(string[] args)
    {

      Point[] p = new Point[3];
      int i;
      makeTriangle(p);
      Console.WriteLine("The triangle consists of the following coordinates:");
      for(i=0 ;i<3;i++)
         Console.WriteLine("({0},{1})\t",p[i].x,p[i].y);


      Console.ReadLine();

    }

   static void makeTriangle(Point[] p){
      p[0].x = 200;
      p[0].y = 20;
      p[1].x = 150;
      p[1].y = 100;
      p[2].x = 300;
      p[2].y = 100;

         }



   }
} Check answer 3. What would be printed from the following C# program?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
   {


    struct Fract
     {
       public int nomi;
       public int deno;
       public Fract(int nomi, int deno)
       {
          this.nomi = nomi;
          this.deno = deno;
         }
      }

   static void Main(string[] args)
    {


     Fract f1=new Fract(1, 2);/* 1/2 */
     Fract f2 = new Fract(2, 5);/* 2/5 */
     Fract result = sum(f1, f2);//sum the fractions
     Console.WriteLine("Result={0}\\{1}", result.nomi, result.deno);  //display the result


     Console.ReadLine();

    }

  static Fract sum(Fract f1, Fract f2)
    {
     return new Fract((f1.nomi * f2.deno) + (f2.nomi * f1.deno), f1.deno * f2.deno);
             }



      }
} Check answer


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.