JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
11 days ago8 views
👨‍💻Programming

Untitled Page

QUESTION 1: Class Creation & Object Instantiation

Question:
Create a class named Car with a public string field color. In the Main method, instantiate an object of Car, set its color field to "Red", and print the color value to the console.

Solution:

using System;

namespace LabTest
{
    class Car
    {
        public string color;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car myObj = new Car();
            myObj.color = "Red";
            Console.WriteLine(myObj.color);
        }
    }
}

Expected Output:
Red


==================================================


QUESTION 2: Methods & State

Question:
Define a Car class containing a public integer field maxSpeed, set to 200 and a public method named FullThrottle() that prints "The car is going as fast as it can!". In Main, create a Car object, output its maxSpeed, and invoke the FullThrottle() method.

Solution:

using System;

namespace LabTest
{
    class Car
    {
        public int maxSpeed = 200;

        public void FullThrottle()
        {
            Console.WriteLine("The car is going as fast as it can!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car myObj = new Car();
            Console.WriteLine($"Max Speed: {myObj.maxSpeed} km/h");
            myObj.FullThrottle();
        }
    }
}

Expected Output:
Max Speed: 200 km/h
The car is going as fast as it can!


==================================================


QUESTION 3: Parameterized Constructor

Question:
Create a Car class with public fields modelName and modelYear. Add a parameterized constructor that accepts and initializes these two values. In Main, construct an instance representing a "Mustang" from 1969 and print its details.

Solution:

using System;

namespace LabTest
{
    class Car
    {
        public string modelName;
        public int modelYear;

        public Car(string name, int year)
        {
            modelName = name;
            modelYear = year;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car ford = new Car("Mustang", 1969);
            Console.WriteLine($"{ford.modelYear} {ford.modelName}");
        }
    }
}

Expected Output:
1969 Mustang


==================================================


QUESTION 4: Encapsulation & Properties

Question:
Create a BankAccount class with a private field _balance and a public property Balance. Implement getter and setter logic such that assigning a negative value prints "Invalid amount!" and leaves the balance unchanged. Demonstrate assigning valid and invalid values in Main.

Solution:

using System;

namespace LabTest
{
    class BankAccount
    {
        private double _balance;

        public double Balance
        {
            get
            {
                return _balance;
            }
            set
            {
                if (value >= 0)
                {
                    _balance = value;
                }
                else
                {
                    Console.WriteLine("Invalid amount!");
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BankAccount account = new BankAccount();

            account.Balance = 500;
            Console.WriteLine($"Current Balance: ${account.Balance}");

            account.Balance = -150;
            Console.WriteLine($"Current Balance: ${account.Balance}");
        }
    }
}

Expected Output:
Current Balance: $500
Invalid amount!
Current Balance: $500


==================================================


QUESTION 5: Basic Inheritance & Method Overriding

Question:
Define a base class Vehicle with a virtual method Honk() that displays "Tuut, tuut!". Create a derived class Car that overrides Honk() to display "Beep, beep!". In Main, instantiate both classes and invoke Honk() on each to show polymorphism in action.

Solution:

using System;

namespace LabTest
{
    class Vehicle
    {
        public virtual void Honk()
        {
            Console.WriteLine("Tuut, tuut!");
        }
    }

    class Car : Vehicle
    {
        public override void Honk()
        {
            Console.WriteLine("Beep, beep!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Vehicle myVehicle = new Vehicle();
            Vehicle myCar = new Car();

            myVehicle.Honk();
            myCar.Honk();
        }
    }
}

Expected Output:
Tuut, tuut!
Beep, beep!
← Back to timeline