QA-JAVA-1

Modified on Thu, 9 Feb, 2023 at 10:12 AM

How do I Declare and initialize a variable?

Declaring and initializing a primitive datatype variable needs to have 4 components. The data type: eg- int, the variable name: eg-var, the assignment operator: i.g-'=' and finally the value it is initialized to: eg-0. All put together, for example- int i = 0;

To initialize a List/ArrayList object, we must use the following Syntax:

Here is a resource where you can check how to initialize different datatypes.



Why is Java Throwing an Unreachable Statement Error? Why are my return statements unreachable?

To return an object use the return keyword. Remember, in any flow inside a non-void method, return is the final point or the end point. It is necessary inside any non-void method and is required to be reachable and has to be present even outside of any conditional statements.

The above statement will throw a unreachable statement error.



How to use the '?' operator in Java?

Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. 
Syntax=> <resultant_variable>= <condition>?<value_if_true>:<value_if_false>; 
Syntax example=> int var1= var2>var3? 10:20;

The above command returns 20 since var2>var3 (i.e: 100>150) will return false. Hence the 2nd option is assigned to var1, which is 20.


What  is the && and || operator in Java?

The AND operator or the '&&' operator can join two conditional statements in such a way that the resultant statement passes only if both the statement is true. Similarly, the OR operator or the '||' operator can join two statements such that only one of the statements must pass for the resultant to pass. Not operator or ‘!’ operator returns the reverse of the boolean expression.

What is the difference between '=' and '==' operator?

The '=' operator is an assignment operator. It is used to assign an object to a variable. We use this operator when we are initializing a variable to some value. On the other hand we use '==' operator when we are comparing two values in a conditional block, for example: in an if else block, or while setting an end condition for a for or while loop.


Why does my program terminate/time out before returning?

This might be a result of an infinite loop running in the program. Such loops can be resultant of a key variable not being incremented properly, or the direction of increment is wrong... i.e., the variable decreases even though the final condition is more than the variable, or vice versa.

Below is an example of an infinite loop:

Can I append to a string, the same way a list is appendable?

No, a string in Java is immutable and cannot be appended. We need to declare a StringBuilder object in order to append characters or other strings to it and then convert that StringBuilder object to a String object before returning the same.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article