Set 1 - Javatpoint 1-100 IQ
Set 1 - Javatpoint 1-100 IQ
Mainly used C++ is mainly used for Java is mainly used for application
for system programming. programming. It is widely used in
window, web-based, enterprise and
mobile applications.
Design Goal C++ was designed for Java was designed and created as
systems and applications an interpreter for printing systems
programming. It was an but later extended as a support
extension of C network computing. It was
programming language. designed with a goal of being easy
to use and accessible to a broader
audience.
Compiler and C++ uses compiler only. Java uses compiler and interpreter
Interpreter C++ is compiled and run both. Java source code is
using the compiler which converted into bytecode at
converts source code into compilation time. The interpreter
machine code so, C++ is executes this bytecode at runtime
platform dependent. and produces output. Java is
interpreted that is why it is
platform independent.
Call by Value C++ supports both call by Java supports call by value only.
and Call by value and call by reference. There is no call by reference in
reference java.
Thread C++ doesn't have built-in Java has built-in thread support.
Support support for threads. It relies
on third-party libraries for
thread support.
unsigned C++ doesn't support >>> Java supports unsigned right shift
right shift operator. >>> operator that fills zero at the
>>> top for the negative numbers. For
positive numbers, it works same
like >> operator.
o Interpreted: Java uses the Just-in-time (JIT) interpreter along with the
compiler for the program execution.
JVMs are available for many hardware and software platforms (so JVM is
platform dependent). It is a runtime instance which is created when we run
the Java class. There are three notions of the JVM: specification,
implementation, and instance.
JRE
More Details.
More Details.
7) What is JIT compiler?
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT
compiles parts of the bytecode that have similar functionality at the same
time, and hence reduces the amount of time needed for compilation. Here
the term “compiler” refers to a translator from the instruction set of a Java
virtual machine (JVM) to the instruction set of a specific CPU.
9) What are the main differences between the Java platform and
other platforms?
There are the following differences between the Java platform and other
platforms.
10) What gives Java its 'write once and run anywhere' nature?
The bytecode. Java compiler converts the Java programs into the class file
(Byte Code) which is the intermediate language between source code and
machine code. This bytecode is not platform specific and can be executed on
any computer.
run it by java A
13) Is delete, next, main, exit or null keyword in java?
No.
15) What if I write static public void instead of public static void?
The program compiles and runs correctly because the order of specifiers
doesn't matter in Java.
1. class Test
2. {
3. public static void main (String args[])
4. {
5. System.out.println(10 + 20 + "Javatpoint");
6. System.out.println("Javatpoint" + 10 + 20);
7. }
8. }
30Javatpoint
Javatpoint1020
Explanation
In the first case, 10 and 20 are treated as numbers and added to be 30. Now,
their sum 30 is treated as the string and concatenated with the
string Javatpoint. Therefore, the output will be 30Javatpoint.
1. class Test
2. {
3. public static void main (String args[])
4. {
5. System.out.println(10 * 20 + "Javatpoint");
6. System.out.println("Javatpoint" + 10 * 20);
7. }
8. }
200Javatpoint
Javatpoint200
Explanation
In the first case, The numbers 10 and 20 will be multiplied first and then the
result 200 is treated as the string and concatenated with the
string Javatpoint to produce the output 200Javatpoint.
In the second case, The numbers 10 and 20 will be multiplied first to be 200
because the precedence of the multiplication is higher than addition. The
result 200 will be treated as the string and concatenated with the
string Javatpointto produce the output as Javatpoint200.
1. class Test
2. {
3. public static void main (String args[])
4. {
5. for(int i=0; 0; i++)
6. {
7. System.out.println("Hello Javatpoint");
8. }
9. }
10. }
The above code will give the compile-time error because the for loop
demands a boolean value in the second part and we are providing an integer
value, i.e., 0.
More Details.
1. class Student3{
2. int id;
3. String name;
4.
5. void display(){System.out.println(id+" "+name);}
6.
7. public static void main(String args[]){
8. Student3 s1=new Student3();
9. Student3 s2=new Student3();
10. s1.display();
11. s2.display();
12. }
13. }
Test it Now
Output:
0 null
0 null
Explanation: In the above class, you are not creating any constructor, so
compiler provides you a default constructor. Here 0 and null values are
provided by default constructor.
More Details.
1. class Test
2. {
3. int i;
4. public Test(int k)
5. {
6. i=k;
7. }
8. public Test(int k, int m)
9. {
10. System.out.println("Hi I am assigning the value max(k, m) to i");
11. if(k>m)
12. {
13. i=k;
14. }
15. else
16. {
17. i=m;
18. }
19. }
20. }
21. public class Main
22. {
23. public static void main (String args[])
24. {
25. Test test1 = new Test(10);
26. Test test2 = new Test(12, 15);
27. System.out.println(test1.i);
28. System.out.println(test2.i);
29. }
30. }
31.
There are many ways to copy the values of one object into another in java.
They are:
o By constructor
o By assigning the values of one object into another
o By clone() method of Object class
In this example, we are going to copy the values of one object into another
using java constructor.
Output:
111 Karan
111 Karan
A constructor must not have a return type. A method must have a return
type.
The Java compiler provides a default constructor The method is not provided by
if you don't have any constructor in a class. the compiler in any case.
The constructor name must be same as the class The method name may or may
name. not be same as class name.
a = 10 b = 15
Here, the data type of the variables a and b, i.e., byte gets promoted to int,
and the first parameterized constructor with the two integer parameters is
called.
1. class Test
2. {
3. int i;
4. }
5. public class Main
6. {
7. public static void main (String args[])
8. {
9. Test test = new Test();
10. System.out.println(test.i);
11. }
12. }
The output of the program is 0 because the variable i is initialized to 0
internally. As we know that a default constructor is invoked implicitly if there
is no constructor in the class, the variable i is initialized to 0 since there is no
constructor in the class.
1. class Test
2. {
3. int test_a, test_b;
4. Test(int a, int b)
5. {
6. test_a = a;
7. test_b = b;
8. }
9. public static void main (String args[])
10. {
11. Test test = new Test();
12. System.out.println(test.test_a+" "+test.test_b);
13. }
14. }
More Details.
41) What are the restrictions that are applied to the Java static
methods?
Two main restrictions are applied to the static methods.
o The static method can not use non-static data member or call the non-
static method directly.
o this and super cannot be used in static context as they are non-static.
1. class A2{
2. static{System.out.println("static block is invoked");}
3. public static void main(String args[]){
4. System.out.println("Hello main");
5. }
6. }
Test it Now
Output: static block is invoked
Hello main
More Details.
1)A method that is declared as static is known as A method that is not declared
the static method. as static is known as the
instance method.
2)We don't need to create the objects to call the The object is required to call
static methods. the instance methods.
4)For example: public static int cube(int n){ return For example: public void
n*n*n;} msg(){...}.
Output
hi !! I am good !!
i = 102
More Details.
Output
10
Output
o Single-level inheritance
o Multi-level inheritance
o Multiple Inheritance
o Hierarchical Inheritance
o Hybrid Inheritance
More Details.
o Inheritance provides code reusability. The derived class does not need
to redefine the method of base class unless it needs to provide the
specific implementation of the method.
o Runtime polymorphism cannot be achieved without using inheritance.
o We can simulate the inheritance of classes with the real-time objects
which makes OOPs more realistic.
o Inheritance provides data hiding. The base class can hide some data
from the derived class by making it private.
o Method overriding cannot be achieved without inheritance. By method
overriding, we can give a specific implementation of some basic
method contained by the base class.
1. class A{
2. void msg(){System.out.println("Hello");}
3. }
4. class B{
5. void msg(){System.out.println("Welcome");}
6. }
7. class C extends A,B{//suppose if it were
8.
9. Public Static void main(String args[]){
10. C obj=new C();
11. obj.msg();//Now which msg() method would be invoked?
12. }
13. }
Test it Now
Compile Time Error
Address.java
Employee.java
Output
111 varun
gzb UP india
112 arun
gno UP india
Output:
animal is created
dog is created
More Details.
1. class Person
2. {
3. String name,address;
4. int age;
5. public Person(int age, String name, String address)
6. {
7. this.age = age;
8. this.name = name;
9. this.address = address;
10. }
11. }
12. class Employee extends Person
13. {
14. float salary;
15. public Employee(int age, String name, String address, float salary)
16. {
17. super(age,name,address);
18. this.salary = salary;
19. }
20. }
21. public class Test
22. {
23. public static void main (String args[])
24. {
25. Employee e = new Employee(22, "Mukesh", "Delhi", 90000);
26. System.out.println("Name: "+e.name+" Salary: "+e.salary+" Age:
"+e.age+" Address: "+e.address);
27. }
28. }
Output
68) What are the differences between this and super keyword?
There are the following differences between this and super keyword.
o The super keyword always points to the parent class contexts whereas
this keyword always points to the current class context.
o The super keyword is primarily used for initializing the base class
variables within the derived class constructor whereas this keyword
primarily used to differentiate between local and instance variables
when passed in the class constructor.
o The super and this must be the first statement inside constructor
otherwise the compiler will throw an error.
1. class Person
2. {
3. public Person()
4. {
5. System.out.println("Person class constructor called");
6. }
7. }
8. public class Employee extends Person
9. {
10. public Employee()
11. {
12. System.out.println("Employee class constructor called");
13. }
14. public static void main (String args[])
15. {
16. Employee e = new Employee();
17. }
18. }
Output
Example:
Output:
More Details.
1. class Adder{
2. static int add(int a,int b){return a+b;}
3. static double add(int a,int b){return a+b;}
4. }
5. class TestOverloading3{
6. public static void main(String[] args){
7. System.out.println(Adder.add(11,11));//ambiguity
8. }}
Test it Now
Output:
Compile Time Error: method add(int, int) is already defined in class Adder
More Details.
Output
More Details.
As displayed in the above diagram, the byte can be promoted to short, int,
long, float or double. The short datatype can be promoted to int, long, float
or double. The char datatype can be promoted to int, long, float or double
and so on. Consider the following example.
1. class OverloadingCalculation1{
2. void sum(int a,long b){System.out.println(a+b);}
3. void sum(int a,int b,int c){System.out.println(a+b+c);}
4.
5. public static void main(String args[]){
6. OverloadingCalculation1 obj=new OverloadingCalculation1();
7. obj.sum(20,20);//now second int literal will be promoted to long
8. obj.sum(20,20,20);
9. }
10. }
Test it Now
Output
40
60
1. class OverloadingCalculation3{
2. void sum(int a,long b){System.out.println("a method invoked");}
3. void sum(long a,int b){System.out.println("b method invoked");}
4.
5. public static void main(String args[]){
6. OverloadingCalculation3 obj=new OverloadingCalculation3();
7. obj.sum(20,20);//now ambiguity
8. }
9. }
Output
Explanation
There are two methods defined with the same name, i.e., sum. The first
method accepts the integer and long type whereas the second method
accepts long and the integer type. The parameter passed that are a = 20, b
= 20. We can not tell that which method will be called as there is no clear
differentiation mentioned between integer literal and long literal. This is the
case of ambiguity. Therefore, the compiler will throw an error.
o The method must have the same name as in the parent class.
o The method must have the same signature as in the parent class.
o Two classes must have an IS-A relationship between them.
More Details.
2) Method overloading occurs Method overriding occurs in two classes that have
within the class. IS-A relationship between them.
3) In this case, the parameters In this case, the parameters must be the same.
must be different.
1. class Base
2. {
3. void method(int a)
4. {
5. System.out.println("Base class method called with integer a = "+a);
6. }
7.
8. void method(double d)
9. {
10. System.out.println("Base class method called with double d ="+d)
;
11. }
12. }
13.
14. class Derived extends Base
15. {
16. @Override
17. void method(double d)
18. {
19. System.out.println("Derived class method called with double d ="
+d);
20. }
21. }
22.
23. public class Main
24. {
25. public static void main(String[] args)
26. {
27. new Derived().method(10);
28. }
29. }
Output
Explanation
1. class A{
2. A get(){return this;}
3. }
4.
5. class B1 extends A{
6. B1 get(){return this;}
7. void message(){System.out.println("welcome to covariant return type");}
8.
9. public static void main(String args[]){
10. new B1().get().message();
11. }
12. }
Test it Now
Output: welcome to covariant return type
More Details.
1. class Base
2. {
3. public void baseMethod()
4. {
5. System.out.println("BaseMethod called ...");
6. }
7. }
8. class Derived extends Base
9. {
10. public void baseMethod()
11. {
12. System.out.println("Derived method called ...");
13. }
14. }
15. public class Test
16. {
17. public static void main (String args[])
18. {
19. Base b = new Derived();
20. b.baseMethod();
21. }
22. }
Output
Explanation
1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4.
5. class Honda extends Bike{
6. void run(){System.out.println("running safely with 100kmph");}
7.
8. public static void main(String args[]){
9. Honda honda= new Honda();
10. honda.run();
11. }
12. }
Test it Now
Output:Compile Time Error
1. class Student{
2. int id;
3. String name;
4. final String PAN_CARD_NUMBER;
5. ...
6. }
More Details.
1. class Main {
2. public static void main(String args[]){
3. final int i;
4. i = 20;
5. System.out.println(i);
6. }
7. }
Output
20
Explanation
Since i is the blank final variable. It can be initialized only once. We have
initialized it to 20. Therefore, 20 will be printed.
97) What is the output of the following Java program?
1. class Base
2. {
3. protected final void getInfo()
4. {
5. System.out.println("method of Base class");
6. }
7. }
8.
9. public class Derived extends Base
10. {
11. protected final void getInfo()
12. {
13. System.out.println("method of Derived class");
14. }
15. public static void main(String[] args)
16. {
17. Base obj = new Base();
18. obj.getInfo();
19. }
20. }
Output
Explanation
100) What is the difference between the final method and abstract
method?
The main difference between the final method and abstract method is that
the abstract method cannot be final as we need to override them in the
subclass to give its definition.