I O T M A: AVA Rogramming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

INSTITUTE OF TECHNOLOGY & MANAGEMENT, ALIGARH

JAVA PROGRAMMING (C-401)


B.C.A. Second Year (4th Semester)
Session: 2022-23

QUESTION BANK (UNIT-1) - SOLUTION

Maintained By – Mr. Arshil Noor


(Assistant Professor)
Department Of Computer Science
&
Engineering
UNIT - 1:
1. JDK stands for ____. Explanation: The Java Runtime Environment
(JRE) is software that Java programs require
A. Java development kit to run correctly. Java is a computer language
B. Java deployment kit that powers many current web and mobile
applications. The JRE is the underlying
C. JavaScript deployment kit technology that communicates between the
D. None of these Java program and the operating system.

Answer: A) Java development kit


Explanation: Java Platform, Standard Edition 3. What makes the Java platform independent?
Development Kit (JDK). The JDK is a A. Advanced programming language
development environment for building
applications, applets, and components using B. It uses bytecode for execution
the Java programming language. C. Class compilation
D. All of these
2. JRE stands for ___. Answer: B) It uses bytecode for execution
A. Java run ecosystem Explanation: And the answer would be, it's
B. JDK runtime Environment because of the JVM. The byte code generated
by source code compilation would run in any
C. Java Runtime Environment operating system, but the JVM present in a
D. None of these machine differs for each operating system. And
this is how java is considered a platform-
Answer: C) Java Runtime Environment independent programming language.
Explanation: Multi-line comments start with /*
and ends with */ .
4. Can we keep a different name for the java class
name and java file name?
A. Yes 7. What is the entry point of a program in Java?
B. No A. main() method
Answer: A) Yes B. The first line of code
Explanation: yes, we compile a java file with a C. Last line of code
different name than the class, provided that
D. main class
there should not be any public class in that file.
If there is any public class in file then in that Answer: A) main() method
case you have to give that name as file name.
But if your class does not contain any public Explanation: In Java programs, the point from
class then you can give any name to you class. where the program starts its execution or
simply the entry point of Java programs is the
main() method.
5. What are the types of memory allocated in
memory in java?
8. Can we write a program without a main method
A. Heap memory in Java?
B. Stack memory A. Yes
C. Both A and B B. No
D. None of these Answer: A) Yes
Answer: C) Both A and B Explanation: Yes, we can execute a java
program without a main method by using a
Explanation: There are only 3 types of memory
static block. Static block in Java is a group of
segment. Stack Segment, Heap Segment and
statements that gets executed only once when
Code Segment.
the class is loaded into the memory by Java
The code section contains your bytecode. The ClassLoader, It is also known as a static
Stack section of memory contains methods, initialization block
local variables, and reference variables. The
Heap section contains Objects (may also
contain reference variables). The Static section
contains Static data/methods.
9. Can the main() method be overloaded in Java?
A. Yes
B. No
Answer: A) Yes
Explanation: The answer is, yes, we can
6. Multiline comment is created using ___. overload main() method in Java.
A. // Method overloading in Java is a feature that
allows a class to have more than one method
B. /* */
with the same name. To identify each method
C. <!-- -- > uniquely, we differentiate each method by
types of arguments or the number of
D. All of these
arguments or order of arguments.
Answer: B) /* */
For example, consider the following: System.out.println("Username is: " +
userName); // Output user input
sum(int a, int b)
}
sum(double a, double b)
}
sum(int a, int b, int c)
The above three methods have the same name
sum(). The first, sum() method contains two 11. Method used to take a string as input in Java?
arguments a and b of type int. The second
A. next()
sum() method also contains the two arguments
a and b but the type of arguments (double) B. nextLine()
differentiate it from the other two methods.
The third sum() method contains the three C. Both A. and B.
arguments a, b, and c of type int, it D. None of these
differentiates itself from the other two
methods. Answer: B) Both A. and B.
Explanation: The Scanner class in java.util
package used for obtaining the input of the
10. Which class in Java is used to take input from primitive types like int, double, etc. and strings.
the user? It is the easiest way to read input in a Java
A. Scanner program, though not very efficient if you want
an input method for scenarios where time is a
B. Input constraint like in competitive programming.
The scanner class consists next() and
C. Applier
nextLine() methods.
D. None of these
In this article, the difference between these two
Answer: A) Scanner methods is discussed.
Explanation: The Scanner class is used to get next() Method: The next() method in java is
user input, and it is found in the java. util present in the Scanner class and is used to get
package. To use the Scanner class, create an the input from the user. In order to use this
object of the class and use any of the available method, a Scanner object needs to be created.
methods found in the Scanner class This method can read the input only until a
documentation. In our example, we will use the space(” “) is encountered. In other words, it
nextLine() method, which is used to read finds and returns the next complete token from
Strings: the scanner.
import java.util.Scanner; // Import the
Scanner class
12. Which of the following is the correct syntax to
create a variable in Java?
class Main { A. var name;
public static void main(String[] args) { B. int name;
Scanner myObj = new C. var name int;
Scanner(System.in); // Create a Scanner object
D. All of these
System.out.println("Enter username");
Answer: B) int name;
Explanation: o declare (create) a variable, you
String userName = myObj.nextLine(); // will specify the type, leave at least one space,
Read user input then the name for the variable and end the line
with a semicolon (;). Java uses the keyword int
for integer, double for a floating point number }
(a double precision number), and boolean for a }
Boolean value (true or false).

A. Hello
B. Bye
Syntax: How to Declare a Variable
C. Error
D. All of these
13. Is string mutable in Java? Answer: B) Bye
A. Yes
Explanation: The argument of if statement
B. No should be Boolean type. By mistake if we are
trying to provide any other data type then we
Answer: B) No
will get compile-time error saying incompatible
Explanation: String is an example of an types. Here the argument is of int type,
immutable type. A String object always therefore we will get compile time error saying
represents the same string. StringBuilder is an error: incompatible types: int cannot be
example of a mutable type. converted to Boolean

A String object is immutable and final in Java,


so whenever you manipulate a String object, it
creates a new String object. String 16. What is type casting in Java?
manipulations are resource consuming, so Java
provides two utility classes for string A. It is converting type of a variable from
manipulations, StringBuffer and StringBuilder one type to another
. StringBuffer and StringBuilder are mutable
classes. B. Casting variable to the class

C. Creating a new variable


14. Which of these is a type of variable in Java?
D. All of these
A. Instance Variable
Answer: A) It is converting type of a variable
B. Local Variable from one type to another
C. Static Variable
Explanation: In Java, type casting is a method
D. All of these or process that converts a data type into
another data type in both ways manually and
Answer: D) All of these
automatically. The automatic conversion is
Explanation: There are three types of variables done by the compiler and manual conversion
in java: local, instance and static. performed by the programmer.

15. What will be the output of following Java 17. Which type of casting is lossy in Java?
code?
A. Widening typecasting
public class Main { B. Narrowing typecasting
public static void main(String[]
args) {
C. Manual typecasting
String str = "Hello";
str = "Bye";
System.out.println(str); D. All of these
Answer: B) Narrowing typecasting
public class Main {
public static void main(String arg[]) {
Explanation: In the case of Narrowing Type
int i;
Casting, the higher data types (having larger for (i = 1; i <= 12; i += 2) {
size) are converted into lower data types if (i == 8) {
(having smaller size). Hence there is the loss of System.out.println(i);
data. break;
}
}
18. Which of the following can be declared as }
final in java? }
A. Class
A. 1
B. Method B. No output
C. Variable C. 8
D. 1357911
D. All of these
Answer: B) No output
Answer: D) All of these
Explanation: The final modifier for finalizing Explanation: ~
the implementations of classes, methods, and
variables. The main purpose of using a class
being declared as final is to prevent the class 21. Can the Java program accept input from the
from being subclassed. If a class is marked as command line?
final then no class can inherit any feature from
A. Yes, using command-line arguments
the final class.
B. Yes, by access command prompt
C. No
19. The break statement in Java is used to ___.
D. None of these
A. Terminates from the loop immediately
Answer: A) Yes, using command-line arguments
B. Terminates from the program
immediately Explanation: Can Java program accept input
from command line?
C. Skips the current iteration
The java command-line argument is an
D. All of these
argument i.e. passed at the time of running the
Answer: A) Terminates from the loop java program. The arguments passed from the
immediately console can be received in the java program
and it can be used as an input.
Explanation: The break statement in Java
terminates the loop immediately, and the
control of the program moves to the next
statement following the loop. It is almost
always used with decision-making statements 22. Object in java are ___.
(Java if...else Statement).
A. Classes
B. References
C. Iterators
D. None of these
20. What will be the output of following Java
code? Answer: B) References
Explanation: A Java object is a member (also 25. 'this' keyword in java is ___.
called an instance) of a Java class. Each object
A. Used to hold the reference of the
has an identity, a behavior and a state. The
current object
state of an object is stored in fields (variables),
while methods (functions) display the object's B. Holds object value
behavior. Objects are created at runtime from
templates, which are also known as classes. C. Used to create a new instance

An object stores its state in fields (variables in D. All of these


some programming languages) and exposes its Answer: A) Used to hold the reference of the
behavior through methods (functions in some current object
programming languages). Methods operate on
an object's internal state and serve as the Explanation: this Keyword in Java is a
primary mechanism for object-to-object reference variable that refers to the current
communication. object. this in Java is a reference to the current
object, whose method is being called upon. You
can use “this” keyword to avoid naming
23. What is garbage collection in java? conflicts in the method/constructor of your
instance/object.
A. Method to manage memory in java
B. Create new garbage values
C. Delete all values 26. What will be the output of following Java
code?
D. All of these
Answer: A) Method to manage memory in java import java.util.Scanner;
Explanation: Garbage collection in Java is the
class ThisKeyword {
process by which Java programs perform
private int a = 4;
automatic memory management. Java private int b = 1;
programs compile to bytecode that can be run
on a Java Virtual Machine, or JVM for short. void getSum(int a, int b) {
When Java programs run on the JVM, objects this.a = a;
are created on the heap, which is a portion of this.b = b;
memory dedicated to the program. System.out.println(this.a + this.b);
}
24. Static variables in java are declared as ___.
}
A. final variables
public class Main {
B. new variables public static void main(String args[]) {
C. Constants ThisKeyword T = new ThisKeyword();
T.getSum(3, 5);
D. All of these }
}
Answer: C) Constants
Explanation: There would only be one copy of A. 5
each class variable per class, regardless of how B. 9
many objects are created from it. Static C. 8
variables are normally declared as constants D. 4
using the final keyword. Constants are
variables that are declared as public/private, Answer: C) 8
final, and static.
Explanation: The above Java program is an B. Literal value
example to demonstrate the use of this
keyword. C. Used in exception handling
D. All of these
Answer: D) All of these
27. Which is the correct absolute path of a file in
Java? Explanation: All of the mentioned points are
true about the Null in Java.

A. C:\Program Files\Java\jdk1.8.0_131\
bin\file_name.txt 30. Which statement is correct for private member
B. C:\Program Files\Java\file_name.txt in Java?
C. C:\Program Files\Java\jdk1.8.0_131\ A. Access outside the class is allowed
file_name.txt
D. C:\Program Files\Java\jdk1.8.0_131\ B. Any class can access
bin\File Handling\file_name.txt
C. Declared using private keyword
Answer: A) C:\Program Files\Java\jdk1.8.0_131\
D. All of these
bin\file_name.txt
Answer: C) Declared using private keyword
Explanation: The private members are
Explanation: ~
declared using the private keyword.

28. What will be the output of following Java 31. What are packages in Java?
code? A. Methods of a friend class
B. Methods of the main class
public class Main {
public static void main(String[] args) { C. Way to encapsulate a group of classes,
StringBuffer sb = new StringBuffer("include"); sub-packages, and interface
sb.append("help");
System.out.println(sb); D. All of these
} Answer: C) Way to encapsulate a group of
} classes, sub-packages, and interface
Explanation: ava packages are the ways to
A. Error
B. include encapsulate a group of classes, sub-packages,
C. help and interface.
D. Includehelp

Answer: D) Includehelp 32. Which of these is a non-access modifier?


A. public
Explanation: The string here is a StringBuffer
hence the contents can be edited which makes B. private
the append method work on it by adding 'help'
to the end of the string. C. native
D. All of these

29. Null in Java is ___. Answer: C) native

A. Reserved keyword Explanation: The native is a non-access


modifier in Java.
36. Which of these literals can be contained in a
float data type variable?
33. What is boolean in Java?
A. -3.4e+050
A. A value consisting of only true and
false value B. +1.7e+308
B. A value consisting of 8 values C. -3.4e+038
C. Truthy value in java D. -1.7e+308
D. All of these Answer: (B) -3.4e+038
Answer: A) A value consisting of only true and Explanation: Range of data type float is 3.4e-
false value 038 to 3.4e+308.
Explanation: In Java, the boolean keyword is a
primitive data type. It is used to store only two
37. When an expression consists of int, double,
possible values, either true or false.
long, float, then the entire expression will get
promoted into a data type that is:
34. Multithreading in java is ___. A. float
A. Executing multiple processes B. double
simultaneously
C. int
B. Creating more threads at a time
D. long
C. Blocking threads
Answer: (B) double
D. All of these
Explanation: if one of the operands is of type
Answer: A) Executing multiple processes float, the whole expression gives a float value.
simultaneously Lastly, if one of the operands is of type double,
the whole expression gives a double value,
Explanation: Multithreaded programming a
because numeric arithmetic expressions are
process in which two or more parts of the same
automatically promoted to a higher data type.
process run simultaneously.

38. Which of the following operators can operate


35. Which of these components are used in a Java
on a boolean variable?
program for compilation, debugging, and
execution? A. &&
A. JDK B. ==
B. JVM C. ?:
C. JRE D. +=
D. JIT A. C & B
Answer: (A) JDK B. A & D
Explanation: DK. Java Development Kit aka C. A, B & D
JDK is the core component of Java
D. A, B & C
Environment and provides all the tools,
executables, and binaries required to compile, Answer: (D) A, B & C
debug, and execute a Java
Explanation: Operator Short circuit AND,
&&, equal to, == , ternary if-then-else, ?:, are
boolean logical operators. += is an arithmetic }
operator it can operate only on numeric values.
A. 3 3 6
B. 2 3 4
39. Out of these statements, which ones are
C. 2 2 3
incorrect?
D. 3 1 6
A. The Brackets () have the highest
precedence Answer: (D) 3 1 6
B. The equal to = operator has the lowest Explanation: ~
precedence
C. The addition operator + and the
subtraction operator – have an equal 41. Which of the following option leads to the
precedence portability and security of Java?

D. The division operator / has A. Bytecode is executed by JVM


comparatively higher precedence as B. The applet makes the Java code secure
compared to a multiplication operator and portable
Answer: (D) The division operator / has C. Use of exception handling
comparatively higher precedence as compared to a
multiplication operator D. Dynamic binding between objects

Explanation: Equal to operator has least Answer: (A) Bytecode is executed by the JVM.
precedence. Brackets () have highest Explanation: Bytecode is executed by JVM is
precedence. Division operator, /, has higher the correct answer.
precedence than multiplication operator.
Addition operator, +, and subtraction operator The output of the Java compiler is bytecode,
have equal precedence. which leads to the security and portability of
the Java code. It is a highly developed set of
instructions that are designed to be executed by
40. What will be the output of the following Java the Java runtime system known as Java
program? Virtual Machine (JVM). The Java programs
executed by the JVM that makes the code
class Output portable and secure. Because JVM prevents the
{ code from generating its side effects. The Java
code is portable, as the same byte code can run
public static void main(String args[]) on any platform.
{
int p = 1; 42. Which of the following is not a Java features?
int q = 2; A. Dynamic
int r = 3; B. Architecture Neutral
p |= 4; C. Use of pointers
q >>= 1; D. Object-oriented
r <<= 1; Answer: (C) Use of pointers
p ^= r; Explanation: Pointers is not a Java feature.
Java provides an efficient abstraction layer for
System.out.println(q + ” ” + q + ” ” + r);
developing without using a pointer in Java.
} Features of Java Programming are Portable,
Architectural Neutral, Object-Oriented,
Robust, Secure, Dynamic and Extensible, etc.
46. Which component is used to compile, debug
and execute the java programs?
43. _____ is used to find and fix bugs in the Java A. JRE
programs.
B. JIT
A. JVM
C. JDK
B. JRE
D. JVM
C. JDK
Answer: C
D. JDB
Explanation: JDK is a core component of Java
Answer: (D) JDB Environment and provides all the tools,
executables and binaries required to compile,
Explanation: The Java Debugger also known
debug and execute a Java Program.
as JDB is used to find and fix bugs in the Java
program.

44. Who invented Java Programming?


A. Guido van Rossum
B. James Gosling 47. Which one of the following is not a Java
feature?
C. Dennis Ritchie
A. Object-oriented
D. Bjarne Stroustrup
B. Use of pointers
Answer: B
C. Portable
Explanation: Java programming was
developed by James Gosling at Sun D. Dynamic and Extensible
Microsystems in 1995. James Gosling is well
Answer: B
known as the father of Java.
Explanation: Pointers is not a Java feature.
Java provides an efficient abstraction layer for
45. Which statement is true about Java? developing without using a pointer in Java.
Features of Java Programming are Portable,
A. Java is a sequence-dependent
Architectural Neutral, Object-Oriented,
programming language
Robust, Secure, Dynamic and Extensible, etc
B. Java is a code dependent programming
language
48. Which of these cannot be used for a variable
C. Java is a platform-dependent
name in Java?
programming language
A. identifier & keyword
D. Java is a platform-independent
programming language B. identifier
Answer: D C. keyword
Explanation: Java is called ‘Platform D. none of the mentioned
Independent Language’ as it primarily works
Answer: C
on the principle of ‘compile once, run
everywhere’.
Explanation: Keywords are specially reserved C. 0.0
words that can not be used for naming a user- D. all of the mentioned
defined variable, for example: class, int, for, Answer: D
etc. Explanation: For floating point literals, we
have constant value to represent (10/0.0)
infinity either positive or negative and also
49. What will be the output of the following Java have NaN (not a number for undefined like
code? 0/0.0), but for the integral type, we don’t have
any constant that’s why we get an arithmetic
exception.
1. class increment {
2. public static void main(String
args[])
3. { 51. Which of the following is not an OOPS
4. int g = 3; concept in Java?
5. System.out.print(++g * 8);
6. } A. Polymorphism
7. } B. Inheritance
C. Compilation
A. 32
B. 33 D. Encapsulation
C. 24
D. 25 Answer: C
Explanation: There are 4 OOPS concepts in
Answer: A Java. Inheritance, Encapsulation,
Polymorphism and Abstraction.
Explanation: Operator ++ has more preference
than *, thus g becomes 4 and when multiplied
by 8 gives 32. 52. What is not the use of “this” keyword in Java?
A. Referring to the instance variable when
a local variable has the same name
50. What will be the output of the following Java
program? B. Passing itself to the method of the same
class
1. class output { C. Passing itself to another methodS
2. public static void main(String
D. Calling another constructor in
args[])
constructor chaining
3. {
4. double a, b,c; Answer: B
5. a = 3.0/0;
Explanation: “this” is an important keyword in
6. b = 0/4.0; java. It helps to distinguish between local
7. c=0/0.0; variable and variables passed in the method as
8.   parameters.
9. System.out.println(a);
10. System.out.println(b);
11. System.out.println(c);
12. }
13. }
A. NaN
B. Infinity
53. What will be the output of the following Java
program?

1. class variable_scope
2. {
3. public static void main(String
args[])
4. {
5. int x;
6. x = 5;
7. {
8. int y = 6;
9. System.out.print(x + " " + y);
10. }
11. System.out.println(x + " " + y);
12. }
13. }

A. Compilation error
B. Runtime error
C. 5 6 5 6
D. 5 6 5

Answer: A
Explanation: Second print statement doesn’t
have access to y , scope y was limited to the
block defined after initialization of x.

You might also like