Saturday, August 1, 2015

Question & Answers to be Remember


Question & Answers to be Remember
Q1. Define the term programming language.
A1. A programming language is a language used to write instructions for a computer to perform.
Q2. What are the characteristics (elements) of a programming language?
A2. The 3 characteristics of a programming language are:
1. A programming language has a set of words. These are known as reserved words.
2. A programming language has syntax. This defines the rules on how different parts of the program can be created. For example, the name of a variable cannot start with a number.
3. A programming language has semantics. This defines how different parts of the program can be used with each other. For example, you can multiply two numbers, but you cannot multiply a word with a number
Q3. Give three examples of programming languages.
A3. There are many, many programming languages. These include Java, C, Python, PHP, JSP, ASP, Basic, and so on.
Q4. Explain the difference between syntax and semantics.
A4.
Syntax
Semantics
Syntax defines the rules – how different parts of the program can be created.
Semantics defines how different parts of the program can be used with each other.
For example, the name of a variable cannot start with a number.
For example, you can multiply two numbers, but you cannot multiply a word with a number.
Syntax is the rules of the language.
Semantics is the grammar.
Q5. Explain the two types of programming languages.
A5. There are two types of programming languages: procedural languages and object-oriented languages.
A procedural language is one where the programmer specifies the exact sequence of steps in order to perform a certain task. Everything happens in sequence. C and BASIC are examples of procedural languages.
An object-oriented language is designed around objects and their behavior. Objects are modeled around their real-life equivalents. For example, consider the ice-cream cone. While you are biting the cone, the actual ice-cream is melting, flowing down the side of the cone, maybe onto your fingers (interacting with another object: the hand). Java and C++ are examples of object-oriented languages. Object-oriented languages are event-driven.
Q6. What is a statement in Java?
A6. The instructions we write in a Java program are called statements. They are like sentences in the English language. Just as every sentence in English ends with a full-stop, every statement in Java ends with a semi-colon.
Whilst we can combine the sentences on a single line to form a paragraph, this is rarely done in Java programs. Each statement is normally written on a separate line. It makes for easier reading.
A group of statements is also called a code-block, a code-snippet, or program code.
Q7. Explain what you understand by a compound statement?
A7. A compound statement is a set of statements enclosed within curly braces { and }. These are normally used as the loop body of a for loop, while loop, and a do…while loop. These are also used as the body of an if … else loop. An example of a compound statement inside a for loop is shown below:
for(int i=1; i<=5; i++)
{ // the two statements below form a compound statement
  System.out.println("Hello");
  System.out.println("Good bye");
}
Q8. Explain what you understand by the Java program cycle.
A8. The Java program cycle defines the creation of a program, its compilation using the compiler, and then its execution using the interpreter.
Step 1: Write a program using a text editor and give it an extension of .java. For example, X.java.
Step 2: Compile the program using the Java compiler (javac.exe on Windows): javac X.java. This checks for syntax and semantic errors, and if all is OK, creates class files. For example, X.class.
Step 3: Execute the class file using the Java runtime interpreter (java.exe on Windows): java X. If this has a public static void main(String[] args) entry-point method, the class file will get executed.
Q9. Explain the need for a Java compiler.
A9.
1. TThe Java compiler checks Java programs for syntax and semantics errors. If there is even one error, the class files are not created. It highlights the errors so that we can rectify them.
2. If there are no errors, then class files are created by the Java compiler. These class files can be executed using the Java interpreter.
Q10. Explain the need for the Java interpreter.
A10. TThe Java interpreter reads the class files created by the Java compiler and executes the code statements inside it.
Q11. Explain bytecode.
A11. Bytecode is a set of instructions that can execute on a Java Virtual Machine (JVM). Bytecode is stored in class files, which have the .class extension.
A Java program is compiled and a class file is created. This class file is not a text file. It cannot be read and understood by us. It is in some other language which is called bytecode.
Bytecode is a set of instructions that can be understood by any machine that can understand class files. The machine may be our computer using the Windows operating system, another computer using the Mac operating system, a third computer using a Unix operating, a portable device such as a mobile phone, any other device that has the required capability to understand class files (including toaster, fridge, washing machine, and so on.) Since the machine may be anything from a computer to a toaster, we call this an imaginary or virtual machine. Since this virtual machine has the capability of reading Java class files, it is called a Java Virtual Machine (JVM).
Q12. Explain the concept of JVM.
A12. The Java Virtual Machine is an imaginary machine that can understand and execute bytecode (class files). This machine may be a computer, a mobile phone, or any other device (toaster, fridge, etc.) The JVM is enabled using something known as the Java Runtime Environment (JRE). This includes the Java interpreter (java.exe on Windows).
The JVM includes not just the Java interpreter but also a standard set of libraries. These libraries are packaged class files – that provide all the necessary functionality – which includes displaying data on the screen, connecting to a database, and so on.
Q13. Explain WORA.
A13. WORA is the promise of Java, and is implemented using the JVM concept. A Java program, when compiled, results in the creation of a class file which is in bytecode. This bytecode can be executed (run) on any device that has the JVM – that device may be a computer, a mobile phone, or even an appliance such as a toaster.
Q14. Is Java a platform or a programming language or both? Explain.
A14. Java is both a platform and a programming language. It is a platform because we can use it to write programs that, when converted into bytecode (class files), can execute on any machine that implements the JVM. It is a programming language because we write programs using Java (which has reserved words, syntax, and semantics).
Q15. Differentiate between Java compiler and Java interpreter.
A15.
Java compiler
Java interpreter
Works on a Java program (.java file)
Works on bytecode (.class file)
Performs syntax and semantics checks of a Java program and highlights errors
Executes the class files created by the Java compiler
If no errors, then creates class files
The Java interpreter forms part of the JVM. This is because the JVM is a machine that can execute bytecode. Bytecode is inside class files. The Java interpreter reads the class files and executes the instructions inside the class files.
Q16. What is the language used in a Java class file called?
A16. Bytecode.
Q17. What are the full forms of the following acronyms?
JVM, JRE, WORA, API
A17.
JVM = Java Virtual Machine
JRE = Java Runtime Environment
WORA = Write Once Run Anywhere
API = Application Programming Interface
Q18.
State whether the following are true or false. Explain.
a. The Java compiler is a part of the JVM.
b. Java can run inside a washing machine.
c. A class file is a text file.
A18.
a. False. The Java compiler converts a Java program into class files. It is not part of the run time environment, and is hence not part of the JVM.
b. True. All that is required is the Java Runtime Environment – which is the Java interpreter and the class files.
c. False. A class file is in bytecode, and is hence not a text file.
Q19. Describe two advantages of Java.
A19.
1. Java is an object-oriented programming language. This means that it is based on the modeling of objects and events, which more realistically represent real-life scenarios. The features of object-oriented programming languages are applicable to Java as well.
2. The Java model is based on byte-code and the JVM concept. This means that the class files can be created once (by a compiler) and run anywhere. This is the WORA (Write Once Run Anywhere) advantage. It means that a class file created on, say, a Windows Vista machine, can be moved and run on any device that has the JRE installed, without compiling it.
Q20. State whether the following are true or false? Explain.
a. The Java interpreter is the JRE.
b. Java is both a platform and a programming language.
A20.
a. False. The JRE is the Java Runtime Environment, and includes the Java interpreter as one of its components. The JRE also includes, besides the Java interpreter, the other class files needed to provide the complete runtime environment.
b. True. Java is both a platform and a programming language. It is a platform because we can use it to write programs that, when converted into bytecode (class files), can execute on any machine that implements the JVM. It is a programming language because we write programs using Java (which has reserved words, syntax, and semantics).
Q21. What is the extension used for Java program files?
A21. Java programs have the .java extension.
Q22. What is the extension of the class file created when you compile a Java program?
A22. Class files have a .class extension.
Q23. Java is a case-sensitive language. True or false? Explain.
A23. True. Java is a case-sensitive language.  This means that we have to be very careful when we write programs, since variables, function names, class names, all are case-sensitive. As an example: A class named First is different from a class named first which is different from a class named FiRsT.
Q24. What type of editors can be used for writing Java programs? Name one such editor.
A24. Text editors can be used for writing Java programs. One such editor is Notepad.
Q25. A Java program is named Titanic.java. What is the command used to compile this program.
A25. The command used is:
javac Titanic.java
Q26. What is the main() method? Explain its significance.
A26. The main() method is the entry-point method of a class. A class can have multiple methods. When the class file is executed using the Java interpreter (java.exe), the code inside the main() method gets executed.
Q27. Why is main() known as the entry-point method of a class?
A27. The main() method is the entry-point method of a class. A class can have multiple methods. When the class file is executed using the Java interpreter (java.exe), the code inside the main() method gets executed. Hence the main() method is known as the entry-point method of a class.
Q28. A class can have only one method. True or false? Explain.
A28. False. A class can have as many methods as we want. There is no practical restriction on the number of methods a class may have. For example, the Math class has methods random() and sqrt().
Q29. A class Zoozoo contains a main() method, and another method named play(). The name of the Java program file is Zoozoo.java.
a. What is the command used to compile this file?
b. What is the name of the resultant class file?
c. On executing this class file, which method will be guaranteed to execute?
A29.
a. javac Zoozoo.java
b. Zoozoo.class
c. The public static void main( String[] args ) method.
Q30. Write the command to compile the following Java program files:
a. X.java
b. Y.java
c. XY.java
d. Car.java
e. Bike.java
f. School.java
A30.
a. javac X.java
b. javac Y.java
c. javac XY.java
d. javac Car.java
e.javac Bike.java
f. javac School.java
Q31. What is the name of the method used to print text on the screen?
A31. There are two methods commonly used to print text on the screen. These are: System.out.println() and System.out.print().
Q32. Explain the difference between the System.out.print() and System.out.println() methods.
A32.
System.out.println()
System.out.print()
The System.out.println() method displays the text on the screen, and then moves the cursor to the start of the next line.
The System.out.print() method displays the text on the screen, and the cursor remains at the end of the displayed text, on the same line.
The next print() or println() statement will display output from the start of the next line.
The next print() or println() statement will display output from the end of the text displayed on the same line itself.
 Q33. Can you use the System.out.print() method to skip a line?
A33. No, the System.out.print() method does not move the cursor to the start of the next line, so we cannot use it to skip a line. We can use the System.out.println() method to skip a line.
Q34.
xamine the code snippet below:
System.out.print(“Good”); System.out.println(“Morning”);
Which of the following options is the correct output?
a.
Good Morning
b.
GoodMorning
c.
Good

Morning
d.
Good
 Morning
A34. The answer is option ‘b’.
Q35. Shown below are different code snippets of print() and println() statements. What will be the outputs for each code snippet?
a.
System.out.print("Good");
System.out.print(" ");
System.out.println("Morning");
b.
System.out.print("Good");
System.out.print(" ");
System.out.println("Morning");
System.out.print("How");
System.out.print("Are");
System.out.println("You?");
c.
System.out.print("Good");
System.out.println(" ");
System.out.print("Morning");
d.
System.out.print("Good");
System.out.print("Morning");
System.out.println();
System.out.println("How are you?");
e.
System.out.print("Good");
System.out.println("Morning");
System.out.println();
System.out.println("How are you?");
f.
System.out.print("Good");
System.out.println("Morning");
System.out.print(" ");
System.out.println("How are you?");
g.
System.out.println("Good Morning");
System.out.println();
System.out.println("How are you?");
System.out.println();
System.out.println();
System.out.println("Nice to meet you.");
A35.a.
Good Morning
b.
Good Morning
HowAreYou?
c.
Good
Morning
d.
GoodMorning
How are you?
e.
GoodMorning

How are you?
f.
GoodMorning
 How are you?
g.
Good Morning

How are you?

Nice to meet you.
Notes. In answer (f) above, please note that there is a space before the word “How”.
Q36. Which of the following statements is not correct? Why?
a. System.out.println();
b. System.out.println(“”);
c. System.out.print();
d. System.out.print(“”);

A36. Option “c” is incorrect. Since the print() method does not move the cursor to the start of the next line, there is no point in having an empty print method. Java does not allow this and will give a compiler error.

No comments:

Post a Comment