Find the output of C#.Net programs | Classes & Objects | Set 2: Enhance the knowledge of C#.Net Classes & Objects concepts by solving and finding the output of some C#.Net programs.
Question 1:
using System;
namespace Demo
{
class Sample
{
int A=5;
int B=10;
public void set(int a, int b)
{
A = a;
B = b;
}
public void print()
{
Console.WriteLine(A + "," + B);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
(new Sample()).print();
}
}
}
Question 2:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public void setEmp(int id, string na, int sal)
{
emp_id = id;
name = na;
sal = salary;
}
public void print()
{
Console.WriteLine(emp_id + "," + name+","+salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
emp.setEmp(10, "Virat", 10000);
emp.print();
}
}
}
Question 3:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
public void setEmp(int id, string na, int sal);
public void print();
}
public void Employee::setEmp(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void Employee::print()
{
Console.WriteLine(emp_id + "," + name+","+salary);
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
emp.setEmp(10, "Virat", 10000);
emp.print();
}
}
}
Question 4:
using System;
namespace Demo
{
class Employee
{
int emp_id;
string name;
int salary;
internal void setEmp(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
internal void print()
{
Console.WriteLine(emp_id + "," + name+","+salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
emp.setEmp(10, "Virat", 10000);
emp.print();
}
}
}
Question 5:
using System;
namespace Demo
{
const class Employee
{
int emp_id;
string name;
int salary;
public void setEmp(int id, string na, int sal)
{
emp_id = id;
name = na;
salary = sal;
}
public void print()
{
Console.WriteLine(emp_id + "," + name+","+salary);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Employee emp = new Employee();
emp.setEmp(10, "Virat", 10000);
emp.print();
}
}
}
Answer 1:
Output:
Explanation:
In the above program, we created a class Sample that contains two private integer data members A and B that are initialized with 5 and 10 respectively. Here, we created two methods set() and print() inside the Sample class.
The set() method is used to set values inside the data members, and the print() method is used to print values of data members on the console screen.
Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.
In the Main() method, we created an anonymous object of Sample class and call method using an anonymous object to print the values of A and B on the console screen.
Answer 2:
Output:
Explanation:
In the above program, we created a class Employee that contains three private data members. Here, we created two methods setEmp() and print() inside the Sample class.
The setEmp() method is used to set values of employee data members, and the print() method used to value of employee object.
Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.
In the Main() method, we created an object of Employee class.
Here, we set employee id, employee name, and employee salary using the setEmp() method, but here we did not assign the value of salary, the below statement is wrong, because we assigned local variable sal by data member salary.
sal = salary;
The correct statement is given below,
salary = sal;
Then, we call print() method to print employee detail.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because we cannot define methods of a class outside the class.
Answer 4:
Output:
Explanation:
In the above program, we created a class Employee that contains three private data members. Here, we created two methods setEmp() and print() inside the Sample class.
Here, we used access specifier internal. The internal members of the class, can be accessed outside the class.
The setEmp() method is used to set values of employee data members, and the print() method used to value of employee object.
Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.
In the Main() method, we created an object of Employee class.
Here, we set employee id, employee name, and employee salary using the setEmp() method and then we call print() method to print employee detail.
Answer 5:
Output:
Explanation:
The above program will generate syntax error because we cannot create a const class in a C# program.
need an explanation for this answer? contact us directly to get an explanation for this answer