Java Why does the output show 1 and not 3?

AI Thread Summary
The Java code in question defines two variables named 'a' with different scopes. The outer variable 'a' is initialized to 1, while the inner variable 'a' is initialized to 3 within the if statement's block. Due to variable scope rules, the inner 'a' does not affect the outer 'a', which remains 1. Therefore, the output of the code is 1, making the correct answer B. The discussion emphasizes the importance of understanding variable scope in Java, highlighting that the inner variable is not accessible outside its block. Additionally, there is a note on the necessity of proper syntax and structure in Java code, as improper placement of braces can lead to compilation errors.
jackylaucf
Messages
3
Reaction score
0
Consider the following java code

Java:
int a=1;
if (a != 0)
{
  int a=3;
             }
System.out.print(a);

What is the output
A. 0
B. 1
C. 3
D. No output. A compilation error occurs

The answer is B. I want to ask why the output shown will not be 3. As the 1 is not equal to 0, should the ' int a ' be modified to 3?
Thank you
 
Last edited by a moderator:
Technology news on Phys.org
I suggest you review the concept of "scope" of a variable. In your code, there are two separate variables named 'a'. One of them is "visible" only outside the curly braces, and contains the value 1. The other one is "visible" only inside the curly braces, and contains the value 3.
 
Pay a bit more attention to where you put your closing braces. At first I thought your code was missing the right brace on the if statement. The closing brace should be aligned with the code that follows it, like so:
Java:
int a=1;
if (a != 0)
{
   int a=3;
}
System.out.print(a);
 
  • Like
Likes FactChecker
jackylaucf, your example as shown doesn't compile if you place it within a method. I'm assuming that you haven't copied it correctly and have left off important info. The answer would be D unless the question looks something like this:
Java:
class SomeJavaClass {
    int a = 1;

    void someJavaMethod() {
        if (a != 0) {
            int a = 3;
        }

        System.out.print(a);
    }
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
2K
Replies
19
Views
2K
Replies
22
Views
3K
Replies
2
Views
3K
Replies
13
Views
4K
Replies
2
Views
6K
Back
Top