Different Kinds of Coding Questions

- Writing some statements/code snippet

If the questions never ask you write a method, class or program, you just need to write the statements and should NOT wrap it with any method or class. An example is given below:

Question: 

Write a FOR loop to print out 1 to 100 in each line

Answer:

for(int i=1; i <= 100; i++) {
   System.out.println(i);
}


- Writing a method

If the question asks you to write a method, you are required to provide the complete method definition, however you should NOT wrap it with any class. An example is given below:

Question: 

Implement a method sayHello that takes in a String as the argument and display "Hello" followed by the argument.

Answer:

public void sayHello(String name) {
    System.out.println("Hello " + name);
}


- Writing a class

If the question asks you to write a class, you are required to include everything, i.e. class definition, field(s) and method(s). An example is given below:

public class MyClass {  //define the class
    private int id; //field     public void show() { //method
         System.out.println("this is a method");
    }
}

- Writing a program

if the question asks you to write a program, you still need to write a class, but you must include the main() method inside.

public class MyClass {  //define the class
    public static void main(String[] args) {
	System.out.println("Welcome to C208 Java!");
    }
}




Last modified: Monday, 17 October 2016, 10:06 PM