0% found this document useful (0 votes)
70 views11 pages

Java Programming Concepts and Questions

The document contains multiple choice questions related to Java programming concepts. It tests knowledge of Java features like platform independence, use of JDK, valid variable names, array properties, operators, OOPS concepts, output of code snippets, and more. There are a total of 75 questions in the document.

Uploaded by

Hemanth M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views11 pages

Java Programming Concepts and Questions

The document contains multiple choice questions related to Java programming concepts. It tests knowledge of Java features like platform independence, use of JDK, valid variable names, array properties, operators, OOPS concepts, output of code snippets, and more. There are a total of 75 questions in the document.

Uploaded by

Hemanth M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Which statement is true about Java?

a) Java is a sequence-dependent programming language


b) Java is a code dependent programming language
c) Java is a platform-dependent programming language
d) Java is a platform-independent programming language

2. Which component is used to compile, debug and execute the java programs?
a) JRE
b) JIT
c) JDK
d) JVM

Which of these cannot be used for a variable name in Java?


a) identifier & keyword
b) identifier
c) keyword
d) none of the mentioned

What is the extension of compiled java classes?


a) .txt
b) .js
c) .class
d) .java

Which of the following is FALSE about arrays on Java?


a) A java array is always an object
b) Length of array can be changed after creation of array
c) Arrays in Java are always allocated on heap

Which one of the following is not a Java feature?


a) Object-oriented
b) Use of pointers
c) Portable
d) Dynamic and Extensible

What will be the output of the following Java code?


public class Main
{
public static void main(String[] args) {
int a = 10;
String temp = [Link](a);
[Link](temp);
}
}
a) compilation error
b) exception
c) 10
d) 1

What will be the output of the following Java code?


class increment {
public static void main(String args[])
{
int g = 3;
[Link](++g * 8);
}
}
a) 32
b) 33
c) 24
d) 25

Java is case sensitive language.


a) True
b) False
Which of the following operator has more precedence?
a) ()
b) ++
c) *
d) >=

Which of the following operators has more precedence in Java?


a) -
b) +
c) *

Which of the following is not an operator in Java?


a) |
b) ^
c) ~
d) <->

Which environment variable is used to set the java path?


a) MAVEN_Path
b) JavaPATH
c) JAVA
d) JAVA_HOME

Which of the following is not an OOPS concept in Java?


a) Polymorphism
b) Inheritance
c) Compilation
d) Encapsulation

What is correct sequence of execution of any Java program?


a) Editing -> Compilation -> Class Loader -> Bytecode Verifier -> Execution
b) Editing -> Bytecode Verifier -> Compilation -> Class Loader -> Execution
c) Editing -> Compilation -> Bytecode Verifier -> Class Loader -> Execution
d) None of the above

What will be the error in the following Java code?


byte b = 50;
b = b * 50;
a) b cannot contain value 50
b) b cannot contain value 100, limited by its range
c) No error in this code
d) * operator has converted b * 50 into int, which cannot be converted to byte without
casting

Find the output of the following program.

public class CppBuzz {


public static void main(String args[]) {

int a =10;
String b = "10+10";

[Link](a+b);

}
}
a) 30
b) 1020
c) 10+10+10
d) 1010+10

Find the output of the following program.


public class CppBuzz {

public static void main(String args[]) {

int a =10;
String b = "10";

[Link](b+a);

}
}
a) 10+10
b) 1010
c) compilation error
d) Undefined

How many times 'Hello' is printed?

public class CppBuzz {


public static void main(String[] args){
for(int i = 0; i<5; i=5 )
{
[Link]("Hello");
}
}
}
a) 5
b) 4
c) 2
d) 1
Find the output of the following program.
public class Main
{
public static void main(String[] args) {
String str = "1234";
int a = [Link](str);
[Link](a);
}
}
a) 123
b) 1234
c) 12340
d) Compilation Error

Find the output of the following program.


public class Main
{
public static void main(String[] args) {
String str = "1234.34";
Float a = [Link](str);
[Link](a);
}
}
a) 1234.00
b) 1234.34
c) 1234
d) Exception

Find the output of the following program.


public class Main
{
public static int ret(){
return 0;
}
public static void main(String[] args) {
int a = 0;
if(ret()==0)
[Link]("Sun");
[Link]("Moon");
}
}
a) sun
b) sunmoon
c) nothing is printed
d) Compilation Error

How many times 'Hello' is printed?


public class CppBuzz {
public static void main(String[] args){
for(int i = 0; i>5; )
{
[Link]("Hello");
}
}
}
a) 0
b) 4
c) 2
d) 1

How many times 'Hello' is printed?


public class CppBuzz {
public static void main(String[] args){
for(int i = 0; i<5; )
{
[Link]("Hello");
}
}
}
a) 0
b) 1
c) 2
d) Infinite times

How many times 'Hello' is printed?


public class CppBuzz {
public static void main(String[] args){
for(int i = 0; i<5; i++)
{
[Link]("Hello");
i++;
i--;
}
}
}
a) 5
b) 4
c) 3
d) 2

Which of these are Access Modifiers in java?


a) default
b) public
c) protected
d) All of these

Which of these are selection statements in Java?


a) break
b) continue
c) for()
d) if()

Find the output of the following program.


public class Solution{
public static void main(String[] args){
short x = 10;
x = x * 5;
[Link](x);
}
}
a) 50
b) 10
c) Compile error
d) Exception

Find the output of the following program.


public class Main
{
public static void main(String[] args) {
int arr[] = {'a','b','c','d','e'};

for(int i = 0 ; i<5; i++){


[Link](" "+ (char)arr[i]);
}
}
}
a) a b c d e
b) 96 97 98 99 101
c) 65 66 67 68 69
d) None of the above

Find the output of the following program.


public class Main
{
public static void main(String[] args) {

int arr[] = {1,2,3,4,5};


int count = 0;

for(int i = 0 ; i<5; i++ ){


if(arr[i]%2==0)
arr[i] *= 2;
[Link](arr[i]);
}

}
}
a) 12345
b) 135
c) 22222
d) 14385

Find the output of the following program.


public class Main
{
public static void main(String[] args) {

int arr[] = {1,2,3,4,5};

int count = 0;

for(int i = 0 ; i<5; i++ ){


if(arr[i]%2==0)
count++;
}
[Link](count);
}
}
a) 3
b) 2
c) 1
d) 0

An array is a group of similar type of datatype.


a) True
b) False

Find the output of the following program.


public class CppBuzz{
public static void main(String[] args){
int a = 10;
[Link](a++);
a++;
}
}
a) 10
b) 11
c) 12
d) 13

Find the output of the following program.


public class MyClass {
public static void main(String[] args){
int a = 10;
[Link](++a++);
}
}
a) 10
b) 11
c) 12
d) Compilation Error

What will be the output of the following Java code?


class box
{
int width;
int height;
int length;
}
class main
{
public static void main(String args[])
{
box obj = new box();
[Link] = 10;
[Link] = 2;
[Link] = 10;
int y = [Link] * [Link] * [Link];
[Link](y);
}
}
a) 100
b) 400
c) 200
d) 12

What will be the output of the following Java code?


public class CppBuzz {
public static void main(String[] args){
int a = 5+5*2+2*2+(2*3);
[Link](a);
}
}
a) 138
b) 264
c) 41
d) 25

What will be the output of the following Java program?


public class CppBuzz {
public static void main(String[] args){
int a = 10;
[Link](a*a--);
}
}
a) 100
b) 90
c) 81
d) 80

What will be the output of the following Java program?


public class CppBuzz{

public static void main(String[] args){


int a = 5;
a +=5;

switch(a){

case 5: [Link]("5");break;
case 10: [Link]("10");
[Link](((a%2 ==0) ? "-even-" : "-odd-"));
default: [Link]("0");

}
}
a) compilation error
b) 10-even-
c) 10-odd-
d) 10-even-0

What will be the output of the following Java program?


public class CppBuzz{
public static void main(String[] args){
int a = 5;
a +=5;
switch(a){

case 5: [Link]("5");break;
case 10: [Link]("10");
[Link](((a%2 ==0) ? "-even-" : "-odd-"));
break;

default: [Link]("0");

}
}
a) compilation error
b) 10-even-
c) 10-even-0
d) 10-odd

What will be the output of the following Java program?


public class CppBuzz{
public static void main(String[] args){
int a = -5;
a +=5;
switch(a){
case 5: [Link]("5");break;
case 10: [Link]("10");
default: [Link]("0");

}
}
a) 5
b) 10
c) 100
d) 0

What will be the output of the following Java program?


1. class Output
2. {
3. public static void main(String args[])
4. {
5. int arr[] = {1, 2, 3, 4, 5};
6. for ( int i = 0; i < [Link] - 2; ++i)
7. [Link](arr[i] + " ");
8. }
9. }
a) 1 2 3 4 5
b) 1 2 3 4
c) 1 2
d) 1 2 3

What will be the output of the following Java program?


1. class Output
2. {
3. public static void main(String args[])
4. {
5. double x = 2.0;
6. double y = 3.0;
7. double z = [Link]( x, y );
8. [Link](z);
9. }
10. }
a) 9.0
b) 8.0
c) 4.0
d) 2.0

Common questions

Powered by AI

When parsing the string "1234.34" into a float in Java using Float.parseFloat(), the output will be 1234.34. This demonstrates Java's ability to handle string-to-float conversions accurately, preserving numeric precision as long as it falls within the float type's precision limits. This conversion is common in scenarios involving reading and processing numeric input from external sources like files or user input .

The absence of pointers in Java is a deliberate choice that simplifies memory management and enhances security compared to languages like C++. By eliminating pointers, Java reduces the risks associated with pointer arithmetic, such as memory corruption and buffer overflow vulnerabilities. This forces developers to use object references, which it manages using a garbage collector that automatically deallocates memory for objects no longer in use. However, this also means that Java developers have less direct control over memory allocation and deallocation, potentially leading to performance overhead due to garbage collection cycles .

Java's syntax and feature set support platform-independent GUI development through libraries like AWT and Swing, which are designed to interact seamlessly with the native operating system’s graphical components. These libraries generate UI elements that function consistently across different systems by abstracting platform-specific details, allowing developers to create graphical interfaces that maintain a consistent look and feel regardless of the underlying platform. This reduces the development time and complexity associated with crafting platform-specific UI components and encourages the development of cross-platform applications .

Java's platform independence means that Java programs can run on any device or operating system that has the Java Virtual Machine (JVM) installed, without requiring platform-specific modifications. This feature significantly impacts software development by allowing developers to write code once and deploy it anywhere, reducing the cost and complexity of developing and maintaining applications across different environments. This characteristic is facilitated by the conversion of Java code into bytecode that the JVM interprets, which abstracts platform-specific details .

Java arrays being fixed in size after creation has several implications for application design and memory usage. Design-wise, developers must predict array size during instantiation, potentially leading to inefficient memory usage if the size is overestimated or buffer overflow errors if underestimated. This characteristic encourages developers to use more flexible collections like ArrayLists when dynamic resizing is essential. Memory usage can be problematic if large arrays are allocated but underutilized, while small arrays can cause frequent reallocation, each needing careful consideration to balance performance, memory usage, and scalability .

Java uses a two-step process for execution that involves compiling the source code into platform-independent bytecode using the Java Compiler, followed by interpretation or just-in-time (JIT) compilation of this bytecode by the Java Virtual Machine (JVM). Unlike directly compiled languages such as C++, which compile directly into machine code for a specific platform, Java’s approach allows the compiled bytecode to be portable across platforms. This offers several advantages: it enhances security, allows Java applications to be environment-agnostic, and simplifies code deployment and updates in heterogeneous environments .

Operator precedence in Java determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence. For example, in the expression 5 + 5 * 2, the multiplication operator (*) has higher precedence than the addition operator (+), so the multiplication is performed first, yielding the result 15. Misunderstanding precedence can lead to unexpected results, so using parentheses to explicitly define order when it is crucial can help prevent errors and improve code clarity .

Java's "write once, run anywhere" (WORA) philosophy significantly influences its support for internet-based applications by enabling developers to create applets or applications that can run on any client machine with a Java-enabled browser or a Java Virtual Machine (JVM). This cross-platform compatibility is crucial for web applications, supporting seamless functionality across various devices and operating systems, thus ensuring a broader reach and interoperability. Java applets were among the early pioneers of client-side programming, allowing for rich, interactive internet-based applications beyond the capabilities of static HTML .

In Java, arrays are considered objects because they inherit from the Object class, which means they have associated metadata such as length and type checks. This object nature leads to arrays being heap-allocated, potentially leading to more controlled memory management and safety checks such as bounds checking. Consequently, while arrays offer safety and flexibility, the overhead of managing them as objects and the associated metadata can impact performance compared to languages that treat arrays as simple data blocks .

The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine (JVM), and other components required to run applications written in Java. The JRE is a part of the Java Development Kit (JDK), which also includes development tools such as the compiler and debugger necessary for developing Java programs. The JVM is the engine that reads and executes the Java bytecode. Together, these components allow developers to write, compile, and execute Java code across different platforms, facilitating Java's 'write once, run anywhere' capabilities .

You might also like