Question

Identify the errors in the following C# program. Write the correct program. Explain the errors. class...

Identify the errors in the following C# program. Write the correct program. Explain the
errors.
class Program
{
static Main(string[] args)
{
int x = 10
Console.WriteLine("{0}" + x);
Console.ReadLine();
}

Homework Answers

Answer #1

The funcrions used in the code have no issues but there are so many syntactical errors in the code provided in question.

Error 1: Main() method and class Program must have modifiers.

I have added access modifier public to main() and class Program.

Error 2: Main() method must have a return type. If it doesn't return anything then void data type should be mentioned.

I have added void return type to main() function.

Error 3: There should be semi colon after declaration of variable x.

I have added semi colon to int x=10;

Error 4:Main() method is ended properly but class has not closed in given code. A '}' (Closing curly brace) is missing at the end of class Program.

I have added a closing curly brace for class Program at the end of code.

The correct C# code is:

using System;

// making Program class available through out the code
public class Program
{
// Main method with no return type
public static void Main(string[] args)
{
        //Declaring a variable x of type integer
        int x = 10;
        //Console.WriteLine() function is used to display output to console
        Console.WriteLine("{0}" + x);
        //Console.ReadLine() function is used to take user input from console
        Console.ReadLine();
}
}

In the code provided in questiontwo functions Console.WriteLine() and Console.ReadLine() are used. The functions syntax and usage is correct but Console.ReadLine() has no application in the program. The read line not assigned to any variable nor the read input is used for any operation. So there is no need of Console.ReadLine() in the program. I haven't removed it in the above code because it is given in question. However if you feel the same, you can remove it.

The output of the above code is:

Hope this answer helps you.

Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the output of the following program? [ Explain the working of the while loop...
What is the output of the following program? [ Explain the working of the while loop in this program as per your understanding. namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int number = 100; while (true) { Console.WriteLine(number); number+=100; if (number <= 1000) continue; else break; } Console.ReadLine(); } } }
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment06 {    class Program    {...
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment06 {    class Program    {        static void Main(string[] args)        {            int age = 10;            if (age >= 16)            {                Console.WriteLine("Error;Since you are " + age + " years old, you are legally unable to obtain a license.");            }            Console.Write("Hit any key to close"); Console.ReadKey(false);       ...
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class Test { int a, b; Test(int i, int j) { a = i; b = j; } void meth(Test s) { s.a *= 2; s.b /= 2; } } class demo { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("Before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("After call: " + ob.a + " " +...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT