Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Variables are the heart of any Java program, holding the data that breathes life into your code. But how well do you understand their nuances and how to use them effectively? Put your knowledge to the test with this engaging quiz that covers fundamental concepts of variables in Java.
Whether you’re a beginner taking your first steps or a seasoned developer looking to solidify your understanding, this quiz will challenge and enlighten you. Prepare to dive deep into a variable declaration, scope, data types, initialization, and more! Let’s test your knowledge.
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 15 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- Current
- Review / Skip
- Answered
- Correct
- Incorrect
-
Question 1 of 15
1. Question
What is the line missing in the following code ?
public class Sample {
public void Demo ()
{
int a = 0;
int b = a + 5;
System.out.println(“B value is: ” + b);
}
public static void main (String args [])
{
Sample obj = new Sample ();
}
}
CorrectIncorrect -
Question 2 of 15
2. Question
What is the error in the program?
class demo {
public static void main ( String args [] )
{
int a;
system.out.println(“Variable value is” + a );
}
}
CorrectIncorrect -
Question 3 of 15
3. Question
What will be the output of the program?
class factorial {
public static void main ( String args [] )
{
int n = 10 , fact = 1;
for ( i = 1 , i < n , i++ )
{
fact = fact*i;
}
system.out.println( “ Factorial of the program is “ + fact );
}
}
CorrectIncorrect -
Question 4 of 15
4. Question
If the entered number is 10. How many times will the for loop be executed?
class loopexample {
public static void main {
int n , sum = 0 ;
Scanner sc = new Scanner(System.in);
system.out.println( “ Enter a number ” );
n = sc.nextint();
for( int i = n , i = 0 , i– )
{
sum = sum + i )
}
system.out.println(“ The sum of “ + n + “ natural numbers is “ + sum );
}
}
CorrectIncorrect -
Question 5 of 15
5. Question
Which of the following is not a correct syntax of a variable name?
CorrectIncorrect -
Question 6 of 15
6. Question
The above given program is an example of which type of Variable?
public class variableDemo {
public void add()
{
int num = 0;
num = num + 7;
System.out.println(“The number is : ” + num);
}
public static void main( String args [] )
{
VariableDemo obj = new VariableDemo();
obj.add(8);
}
}
CorrectIncorrect -
Question 7 of 15
7. Question
What will be the output of the program?
public class InstanceVariableExample {
public static void main( String args [] )
{
Employee employee = new Employee();
System.out.println(employee.companyName);
System.out.println(employee.branches);
System.out.println(employee.noofemp);
employee.companyName = “DataFlair”;
employee.branches = 10;
employee.noofemp = 28;
}
}
class Employee {
public int branches;
public String companyName;
public int noofemp;
}
CorrectIncorrect -
Question 8 of 15
8. Question
What will be the output of the program?
public class VariableExample{
static int data = 30;
public static void main( String args [] )
{
VariableExample obj = new VariableExample();
System.out.println( VariableExample.data);
}
}
CorrectIncorrect -
Question 9 of 15
9. Question
When will a variable be destroyed?
CorrectIncorrect -
Question 10 of 15
10. Question
How could a variable space be destroyed from the memory?
CorrectIncorrect -
Question 11 of 15
11. Question
What happens to variable a in this program?
class example{
public static void main ( String args [] )
{
int a;
a = 10;
system.out.println(“Value of variable a is “ + a );
a = null;
}
}
CorrectIncorrect -
Question 12 of 15
12. Question
How to call a garbage collector meanwhile in a program?
CorrectIncorrect -
Question 13 of 15
13. Question
What will be the output of the program?
class typecastexample {
public static void main ( String args [] )
{
int i,j,k;
i = 11;
j = 12;
k = (float) i/j;
system.out.println(k);
}
}
CorrectIncorrect -
Question 14 of 15
14. Question
Why is the highlighted line used in this program?
public class demoprogram {
public static void main( String args [] )
{
int x = 99;
System.out.println(((Object)x).getClass().getSimpleName());
}
}
CorrectIncorrect -
Question 15 of 15
15. Question
What can be done to change a variable to a constant?
CorrectIncorrect
Summary:
Congratulations on completing the Java Variables Quiz! Take a moment to reflect on the concepts you’ve explored and identify any areas where you’d like to deepen your understanding. Remember, variables are foundational building blocks in Java, and a strong grasp of them is essential for crafting effective code.
Continue your journey by exploring additional resources, practising regularly, and engaging with the vibrant Java community. The path to Java mastery awaits!
