Short Answers Type Questions:

Short Answers Type Questions:

1.Write the java statement that assigns 1 to x if y is greater than 0
Ans:   if (y > 0) x = 1;
2. Suppose that score is a variable of type double. Write the java statement that increases
the score by 5 marks if score is between 80 and 90
Ans: if (score >= 80 && score <=90) score += 5;

3. Rewrite in Java the following statement without using the NOT (!) operator:
item = !( (i<10) | | (v>=50) )
Ans: item = i >= 10 && i < 50

4. Write a java statement that prints true if x is an odd number and positive
Ans:  if (x % 2 != 0 && x > 0) System.out.println(true);
or
System.out.println(x%2 !=0 && x>0); // This prints false otherwise

5. Write a java statement that prints true if both x and y are positive numbers
Ans:  if (x > 0 && y > 0) System.out.println(true);
or
System.out.println(x > 0 && y > 0); // This prints false otherwise

6. Write a java statement that prints true if x and y have the same sign (-/+)
Ans:  if (x * y > 0) System.out.println(true);
or
System.out.println(x * y > 0); // This prints false otherwise

7. Explain the following terms:
a) IDE
b) Form
Ans:
a) IDE : IDE is an acronym for Integrated Development Environment which is a work
environment that integrates all tools necessary for Application Development
and makes them available as part of one environment.
b) Forms: Forms are used to accept data (input) and submit data to an external agent for
processing.
2. Explain the usage of the following methods :
a) setText()
b) toString()
c) concat()
Ans:
a) setText() : It is used to change the display text of a component (label, text field or button) during run time.
b) toString() : It is used to convert an Integer value to String type.
c) concat() : The concat() method or the string concatenation symbol(+) may be used to
add two strings together.
3. Differentiate between:
Ans:
a) Text field and Text area components :
The Text Field allows the user to enter a single line of text only. But Text Area component allows to accept
multiline input from the user or display multiple lines of information.
b) Text field and Password field components:
The Text Field displays the obtained text in unencrypted form whreas password field displays the obtained
text in encrypted form. This component allows confidential input like passwords which are single line.
c) parseInt() and parseDouble() methods:
parseInt() is used to convert a string value to Integer type whereas parseDouble() is used to convert a
string value to type Double.

8. What is a Variable?
Ans: Variables are named temporary storage locations.

9. Why are data types important?
Ans: Data Types define the way the values are stored, the range of the values and
the operations that can be performed on that type.

10. How are keywords different from variable names?
Ans: Keywords have special meaning in java, should not be used as the variable names. Variables are
named temporary storage locations.
7. What is an identifier?
Ans:Identifiers are fundamental building block of a program and are used as the general terminology for the names given to different parts of the program viz. variables, objects, classes,functions, arrays etc.
8. What is casting? When do we need it?
Ans:Casting is a conversion, which uses the cast operator to specify the type name in parenthesis and is placed in front of the value to be converted.
For example: Result = (float) total / count ;
They are helpful in situations where we temporarily need to treat a value as another type.
9. What is the purpose of break statement in a loop?
Ans:In a loop, the break statement terminates the loop when it gets executed.

10. Is Java case sensitive? What is meant by case sensitive?
Ans: Yes java is case sensitive. Case sensitive means upper case letters and lower case letters are treated
differently.

11. Is a string containing a single character same as a char?
Ans: No

12. What is the main difference between a combo box and a list box?
Ans: The List Box does not have a text field the user can use to edit the selected item, wheras a Combo
Box is cross between a text field and a list.

13. Explain the use of for statement along with its syntax.
Ans: The for loop repeat a set of statements till a test condition is satisfied.
The syntax of the for loop is:
Syntax
for( initialization; test exp; increment/decrement exp)
{
statements;
}
14. What is the difference between selection and repetition?
Ans: Selection statements test conditions and selectively execute code depending on the outcome of the test condition , whereas repetition statements repeat a set of statements till a test condition is satisfied.

15. What is the purpose of if statement? Name the different forms of if statement.
Ans: The if statement allows selection (decision making) depending upon the outcome of a condition.
a) simple if
b) if else
c) if - else if – else

16. What is the purpose of default clause in a switch statement?
Ans: The default statement gets executed when no match is found in switch.

17. What is the main difference between a while loop and a do while loop?
Ans: In while loop test expression is evaluated at the beginning of the loop whereas in do while loop the test expression is evaluated at the bottom of the loop. This means that do-while loop is executed at least once.
18. How is the if…else if combination more general than a switch statement?
Ans:The switch statement must be by a single integer control variable, and each case section must
correspond to a single constant value for the variable. The if…else if combination allows any kind of
condition after each if.

19. Excessive comments add time to the execution of your program. (True/False).
Ans: False because comments are non executable.

20. Differentiate between compile time and run time errors.
Ans: Compile time errors refer to the errors that violate the grammatical rules and regulations of
programming language.

21. Which error is harder to locate and why?
Ans: Logical errors because in presence of logical error , the program executes without any problems but the output produced is not correct. Therefore every statement of the program need to be scanned.

22. Explain the following terms:
a) Exception handling : Run time errors are also called exceptions, and handling such errors in the
application is called exception handling.
b) Syntax : Formal set of rules defined for writing any statement in a language is known as syntax.
c) Portability : The application should be portable. It should be able to run on different platforms.
d) Prettyprinting : Prettyprinting is the formatting of a program to make it more readable. These
formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar
modifications intended to make the content easier to view, read and understand.
e) Syntax error: Syntax errors occur when syntax rules of any programming language are violated. These

errors occur during compilation of the application