java
java
2. NetBeans provides various components used to create a GUI front-end interface like
___________.
a. jTextArea
b. jLabel
c. jButton
4. We should use meaningful names for controls on the _________ in the code. It makes
programming convenient.
a. Form
b. Variables
c. Both a) and b)
9. The __________ selects among a set of statements depending on the value of a controlling
expression.
a. If Statement
b. For Statement
c. While Statement
16. if the condition is false in if condition, which of the following will be true.
a. IF Statement will be executed
b. ELSE Statement is executed.
c. IF and ELSE both will not execute.
17. How many lines of code can be written inside an IF, ELSE, or IF-ELSE block in Java?
19. How many ELSE-IF statements can be present between the beginning IF and the closing ELSE
statements?
20. Select the proper Java IF statement syntax from the list below.
a. if(condition) //statement
b. if(condition){ //statement}
c. if(condition){ //statement1 //statement2}
21. What does the Java programme with IF-ELSE statements produce?
if(TRUE)
System.out.println(“True”);
else
System.out.println(“False”);
a. True
b. False
c. Compiler error
31. The code in the Java programming language is put inside ___________.
a. Blocks
b. Methods
c. Classes, Interfaces
32. What file extension is associated with the Java class source code?
a. .jpp
b. .jsp
c. .java
33. What file extension is associated with compiled Java class files?
a. .jpp
b. .java
c. .class
Que 14. How can you write multiline comments in java program?
Answer: In Java, the multi-line comment can be written by /* ….. */. You need to start the comment by /* and
end by */.
For example:
/* This is an
example of
a multiline comment. */
Que 15. What is package?
Answer: A package in java is a group of related classes. All classes in a package can share their data and code.
Package declaration statement is the first statement in the java program.
package MyJavaProgram ;
Que 16. Why main() is special method of java?
Answer: main() function is very important component of java application program, because the execution of
program is always started from the main() function.
Que 19. Expand IDE. Name any one open source IDE for writing Java programs.
Answer: IDE stands for Integrated Development Environment.
Open Source IDE for writing Java Program – NetBeans, Eclipse, IntelliJ IDEA, Code::Blocks, etc.
Que 20. Which symbol is used in jave at the end of the statement?
Answer: The semicolon ; at the end of the statement. All Java statements must end with a semicolon.
Que 22. What are comment entries in Java? Mention two ways to give comments. [SQP]
Answer: Comments are used in code by programmers to document their programs – to provide explanatory notes
to other people who read the code. This is especially useful in the real world, where large programs are written by
one programmer and maintained by other programmers.
Beginning a comment line with two consecutive forward slashes (//)
Writing the comment between the symbols /* and */
Que 23. Why Java program is platform independent? [SQP}
Answer: A Java compiler instead of translating Java code to machine language code, translates it into Java
Bytecode. A Java interpreter, called the Java Virtual Machine (JVM), translates the bytecode into machine code
and then executes it. The bytecode can be run on any platform as long as it has a JVM running on it. This makes
Java programs platform independent and highly portable. (keywords bytecodes and JVM to be included)
Que 24. How can you write multiline comments in java program? [SQP]
Answer: Writing the comment between the symbols /* and */
Que 25. Why main is a special method in the java program? [SQP]
Answer: Main is a special method that every Java application must have. When you run a program, the
statements in the main method are the first to be executed
Answer: Bytecode is a java compiled code, which translated by java compiler from java source code to java
bytecode. Java Bytecode is a highly optimized set of instructions. Bytecode is also known as Java class file.
When the bytecode is to be run on a computer, a Java interpreter, called the Java Virtual Machine (JVM),
translates the bytecode into machine code and then executes it.
The advantage of such an approach is that once a programmer has compiled a Java program into bytecode, it can
be run on any platform (say Windows, Linux, or Mac) as long as it has a JVM running on it. This makes Java
programs platform independent and highly portable.
Q2. Explain the difference between a Class and an Object with an example.
Answer: Class :- A class is a physical or logical entity that has certain attributes, known as Data members and
Method members to get, set or update the class data members.
For Example Book is a Class which has title, author, publisher, genre and price as the data members and
display_details(), getPrice(), setPrice() etc, are the Member methods.
Objects:- Object is the instance of class i.e. implementation of class physically. Each object has their own data
members. To implement the class we need to create instance of class i.e. variable of class type is called object.
A class as a template from which objects are created.
All objects of the same class have the same type of data members.
Method members of a class are invoked on an object to perform the action associated with that method.
Answer: Constructor is a special method member of class, which is used to initialize the data members of the
class, when you create the instance of class.
Properties of Constructor:
The constructor has the same name as the class.
It has no return type, and
It may or may not have a parameter list.
Whenever a new object of a class is created, the constructor of the class is invoked automatically.
We do not call the constructor explicitly.
Answer: Exception is an error situation that is unexpected in the program execution and cause it to terminate
unexpectedly. To avoid this condition, you need to handle the exception properly.
To handle the exceptions in Java, you need to use try .. catch block. The basic idea in exception handling is
to –Denote an exception block – Identify areas in the code where errors can occur
Catch the exception – Receive the error information
Handle the exception – Take corrective action to recover from the error
try – A try block surrounds the part of the code that can generate exception(s).
catch – The catch blocks follow a try block. A catch block contains the exception handler – specific code that is
executed when the exception occurs. Multiple catch blocks following a try block can handle different types of
exceptions.
finally – The optional finally block is always executed when the try block exits. This means the finally block is
executed whether an exception occurs or not.
Note:- Programmers use this block to put in clean up code, for example, freeing up resources allocated in the try
block.
try {
// Part of the program where an exception might occur
}
catch (exceptiontype1 argument1) {
// Handle exception of the exceptiontype1
}
catch (exceptiontype2 argument2) {
// Handle exception of the exceptiontype2
}
finally {
//Code to be executed when the try block exits
}
Q5. How can threads be created in Java? Briefly explain with an example.
Answer: A multithreaded program is one that can perform multiple tasks concurrently so that there is optimal
utilization of the computer’s resources. A multithreaded program consists of two or more parts called threads each
of which can execute a different task independently at the same time.