Repository navigation
Expand file tree
/
Copy pathjava_interview_questions_javatpoint.rtf
More file actions
3896 lines (3784 loc) · 127 KB
/
Copy pathjava_interview_questions_javatpoint.rtf
File metadata and controls
3896 lines (3784 loc) · 127 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf610
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fswiss\fcharset0 ArialMT;\f2\fnil\fcharset0 Menlo-Regular;
\f3\froman\fcharset0 Times-Roman;}
{\colortbl;\red255\green255\blue255;\red66\green72\blue77;\red255\green255\blue255;\red0\green0\blue0;
\red0\green0\blue0;\red26\green28\blue40;\red36\green149\blue255;\red66\green72\blue77;}
{\*\expandedcolortbl;;\cssrgb\c32549\c35294\c37647;\cssrgb\c100000\c100000\c100000;\cssrgb\c0\c0\c0\c59361;
\cssrgb\c0\c0\c0;\cssrgb\c13333\c14902\c20784;\cssrgb\c16078\c65882\c100000;\cssrgb\c32549\c35294\c37647;}
{\*\listtable{\list\listtemplateid1\listhybrid{\listlevel\levelnfc23\levelnfcn23\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{disc\}}{\leveltext\leveltemplateid1\'01\uc0\u8226 ;}{\levelnumbers;}\fi-360\li720\lin720 }{\listname ;}\listid1}}
{\*\listoverridetable{\listoverride\listid1\listoverridecount0\ls1}}
\paperw11900\paperh16840\margl1440\margr1440\vieww12560\viewh13900\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\b\fs24 \cf0 1. What is Java?
\b0 \
Java is:\
1. High-level\
2. Object-oriented\
3. Platform independent\
4. High performance\
5. Multithreaded\
6. Portable\
programming language & platform (it provides own JRE and API)\
\
\b 2. What are the differences between C++ and Java?
\b0 \
C++\
1. Platform dependent\
2. Mainly used for system programming\
3. Design goal: Designed for systems and application programming\
4. Supports goto\
5. Supports multiple inheritance\
6. Supports operator overloading\
7. Supports pointers\
8. C++ is compiled to machine code (uses compiler only)\
9. Supports both call by value and call by reference\
10. Supports structures and unions\
11. Does not have built in support for threads\
1. Relies on third party libraries\
12. Does not support documentation comments\
13.
\b virtual
\b0 keyword is supported that gives the flexibility to whether override a function or not\
14. Doesn\'92t support unsigned right shift operator >>>\
15. Independent inheritance trees get created (no root that connects all the trees)\
16. Object aware language\
Java\
1. Platform independent (byte code can run on any platform that supports JVM)\
2. Mainly used for application programming\
1. Window applications\
2. Web-based applications\
3. Enterprise applications\
4. Mobile applications\
3. Design goal: Initially designed for printing systems but later extended to support network computing\
1. Goals:\
1. Easy to use\
2. Accessible to broader audience\
4. Java doesn\'92t support goto\
5. No multiple inheritance through class (but can be achieved using interface)\
6. Does not support operator overloading\
7. Internal support for pointer (no explicit pointer programs allowed in Java)\
8. Users compiler and interpreter\
1. Source code is compiled to byte code during compilation time\
2. Bytecode is executed by interpreter at runtime to produce output\
1. Compiles to native code if necessary\
9. Call by value only (no call by reference)\
10. No support for structures and unions\
11. built-in thread support\
12. Supports documentation comments /** \'85 */\
13. No
\b virtual
\b0 keyword support\
1. All non-static methods can be overridden by default (non-static methods are virtual by default)\
14. Supports unsigned right-shift operator >>>\
1. Fills zeros at top of negative numbers or positive numbers\
15. Only single inheritance tree is supported\
1. All classes are child of
\b Object
\b0 class (which is root of the inheritance tree)\
16. Object oriented language\
1. Except primitive types, everything else is an object \
\
\b 3. List the features of Java Programming Language.
\b0 \
1. \ul Simple\ulnone : \
1. Easy to learn\
2. Syntax is similar to that of C++\
2. \ul Object-oriented:\ulnone \
1. Code can be a combination of different type of objects that encapsulates data and behaviour and exchange messages with each other\
3. \ul Portable:\ulnone \
1. write-once-run-anywhere approach\
1. Java program can be converted to bytecode which can be run on any machine that supports JVM\
4. \ul Platform independent:\ulnone \
1. Java comes with it\'92s own platform (JVM) on which code is executed (It is OS independent for execution)\
5. \ul Secured:\ulnone \
1. Doesn\'92t use explicit pointers\
2. Bytecode makes it secure\
3. Exception handling also makes it secure\
6. \ul Robust:\ulnone \
1. Java uses memory management\
1. Automatic garbage collection\
2. Exception handling\
7. \ul Architecture Neutral:\ulnone \
1. Java does not depend on the architecture\
1. Size of data types is fixed (unlike in C or C++ where int can be 32 bit or 64 bit based on the architecture)\
8. \ul Interpreted:\ulnone \
1. Java uses JIT compiler and interpreter to interpret initially and compile if necessary (based on whether code is re-used multiple times)\
9. \ul High Performance:\ulnone \
1. Java is faster than other traditional interpreted languages\
1. Java bytecode is close to native code\
2. bytecode gets compiled to native code over time based on certain heuristics\
10. \ul Multithreaded:\ulnone \
1. Multiple threads can be used to run in parallel out of the box\
1. Multiple threads do not require additional memory (they share common memory area)\
2. Use cases:\
1. Multimedia\
2. Web applications\
11. \ul Distributed:\ulnone \
1. Java facilitates users to construct distributed applications\
1. RMI\
2. EJB\
12. \ul Dynamic:\ulnone \
1. Dynamic loading of classes\
1. Classes are loaded on demand\
2. Supports functions from native langurs - C or C++\
\
\b 4. What do you understand by Java virtual machine?
\b0 \
1. It is a virtual machine that runs java program\
2. It is a runtime engine that calls main method\
3. JVM is a specification which interprets bytecode and converts it to machine code to execute on the machine\
\
\b 5. What is the difference between JDK, JRE, and JVM?
\b0 \
1. JVM\
1. Java Virtual Machine\
2. It is abstract machine the provides runtime environment in which Java bytecode can run\
3. The specification of JVM is maintained by Oracle\
4. Implementations of JVM specification is provided by Oracle and other companies\
5. Implementation of JVM is called JRE\
6. JVM is platform dependent\
2. JRE (JVM + Libraries)\
1. Java Runtime Environment\
2. It is the implementation of JVM\
3. It provides runtime environment for a Java application\
4. Content\
1. Libraries\
2. Files used by JVM at runtime\
3. JDK (JRE + Dev tools)\
1. Java Development Kit\
2. It is software development environment\
1. Provides tools for Java application development\
3. JDK implements any of the following:\
1. Standard Edition Java Platform\
2. Enterprise Edition Java Platform\
3. Micro Edition Java Platform\
\
\b 6. How many types of memory areas are allocated by JVM?
\b0 \
1. Types:\
1.
\b Class (Method) Area:
\b0 \
1. Per-class structures\
1. Runtime constant pool\
2. Field\
3. Method data\
4. Code for methods\
2.
\b Heap:
\b0 \
1. Runtime data area in which memory is allocated to the objects\
3.
\b Stack:
\b0 \
1. Stores frames\
1. Holds local variables, partial results\
2. Used for method invocation and return\
3. Each thread has a private JVM stack\
1. It is created at the same time as the thread\
4. New frame is created each time a method is invoked\
5. Frame is destroyed when its method invocation completes\
4.
\b Program Counter Register:
\b0 PC (program counter) contains address of Java virtual machine instruction currently being executed\
5.
\b Native Method Stack:
\b0 Supports all native methods used in the application (conventional stack of \'93c stack\'94 for instance)\
\
\b 7. What is JIT compiler?
\b0 \
1. Just-In-Time (JIT) Compiler\
1. It is used to improve performance\
2. JIT compiles part of code that is used many times using higher levels of optimisation and to native code (which may be stored in the code cache)\
3. Compilation: Translation of JVM instruction set to CPU instruction set\
\
\b 8. What is the platform?
\b0 \
1. It is hardware or software environment in which a piece of software is executed\
2. Two types:\
1. Software-based - Java provides this type\
2. Hardware based\
\
\b 9. What are the main differences between the Java platform and other platforms?
\b0 \
1. Java platform is provided on top of hardware platform\
2. Java platform is software based\
\
\b 10. What gives Java its \'91write once and run anywhere\'92 nature?
\b0 \
1. Java compiler converts source code into bytecode\
1. Bytecode - it is an intermediate language between source code and machine code\
2. Bytecode is not platform specific (OS + Hardware) and can be run on any computer\
\
\b 11. What is class loader?
\b0 \
1. It is a subsystem of JVM that loads class files\
1. If we need to run a Java program, it is first loaded by the class loader\
2. Types of class loader\
1.
\b Bootstrap ClassLoader
\b0 \
1. First class-loader\
2. It is a super class of
\b Extension ClassLoader
\b0 \
3. It loads
\b rt.jar
\b0 file
\b \
rt.jar
\b0 - contains all classes of Java Standard Edition (JSE)\
1.
\b java.lang
\b0 classes
\b \
\b0 2.
\b java.net
\b0 classes
\b \
\b0 3.
\b java.util
\b0 classes\
\b
\b0 4.
\b java.io
\b0 classes\
5.
\b java.sql
\b0 classes\
6. \'85
\b \
\b0 2.
\b Extension ClassLoader
\b0 \
1. Child class loader of Bootstrap\
2. Parent class loader of
\b System
\b0
\b ClassLoader
\b0 \
3. Loads jar files located inside
\b $JAVA_HOME/jre/lib/ext
\b0 directory\
\
cldrdata.jar\
dnsns.jar\
access.jar\
jfxrt.jar\
localedata.jar\
meta-index\
nashorn.jar\
sunec.jar\
sunjce_provider.jar\
sunpkcs11.jar\
zipfs.jar \
\b \
\b0 3.
\b System/Application ClassLoader
\b0 \
1. Child classloader of Extension\
2. Loads class files from classpath\
1. CLASSPATH - set to current directory by default\
1. Can be changed using:\
1. -cp \
2. -classpath\
\
\b 12. Is Empty .java file name a valid source file name?
\b0 \
1. Yes\
\
class A \{ // no public modifier\
public static void main(String[] args) \{\
System.out.println(\'93Alhamdulillah!\'94);\
\}\
\}\
\
\b 13. Is delete, next, main, exit or null keyword in java?
\b0 \
1. No\
\
\b 14. If I don\'92t provide any arguments on the command line, then what will the value stored in the String array passed into the main() method be, empty or NULL?
\b0 \
1. Empty\
\
\b 15. What if I write static public void instead of public static void?
\b0 \
1. Order of the specifiers does not matter\
\
\b 16. What is the default value of the local variables?
\b0 \
1. primitives - not initialized to any default value\
1. Any usage will throw a compilation error\
2. Object references - not initialised to any default value\
1. Any usage will throw a compilation error\
\
\b 17. What are the various access specifiers in Java?
\b0 \
1.
\b public
\b0 : class, method or variable can be accessed by any class or method\
2.
\b protected
\b0 : accessed by class of same package, subclass of this class (same or different package), same class\
3. default: accessed within package - if no qualifier is given\
4.
\b private
\b0 : class, method or variable can be accessed within the class only\
\
\b 18. What is the purpose of static methods and variables?
\b0 \
1. They are shared among all objects of the class\
2. It is part of the class (not part of the object)\
3. static variables\
1. Created in class area\
1. Objects don\'92t have to be created to access them\
4. Example: class simulating collection of students in college\
1. college name can be static\
\
\b 19. What are the advantages of Packages in Java?
\b0 \
1. To avoid name clashes\
2. Access control (using access modifiers)\
1. Hidden classes can be used by the package\
3. Makes it easy to locate classes using fully qualified names\
\
\b 20. What is the output of the following Java program?
\b0 \
\
class Test \{\
public static void main (String args[]) \{\
System.out.println(10 + 20 + \'93Javatpoint\'94);\
System.out.println(\'93Javatpoint\'94 + 10 + 20);\
\}\
\}\
\
> 30Javatpoint\
> Javatpoint1020\
\
1. First - 10 & 20 are treated as numbers\
2. Second - 10 and 20 are converted to strings\
\
\b 21. What is the output of the following Java program?
\b0 \
\
class Test \{\
public static void main(String args[]) \{\
System.out.println(10 * 20 + \'93Javatpoint\'94);\
System.out.println(\'93Javatpoint\'94 + 10 * 20);\
\}\
\}\
\
> 200Javatpoint\
> Javatpoint200\
\
1. precedence of * is higher than addition hence it will be evaluated first\
\
\b 22. What is the output of the following Java program?
\b0 \
\
class Test \{\
public static void main(String args[]) \{\
for (int i = 0; 0; i++) \{\
System.out.println(\'93Hello Javatpoint\'94);\
\}\
\}\
\}\
\
1. Compilation error (condition in for loop is not boolean)\
\
OOPs Concepts:\
\b 23. What is object-oriented paradigm?
\b0 \
1. Programming paradigm that is based on objects\
1. Objects contain data and methods defined in class to which the object belongs\
2. pros:\
1. Modularity\
2. Re-usability\
3. Objects interact with each other to form the entire program\
4. Features:\
1. Follows bottom-up approach in program design (?)\
2. Encapsulates and abstracts and hides the complexity from user and show only functionality\
5. Examples:\
1. C++\
2. Simula\
3. Smalltalk\
4. Python\
5. C#\
6. \'85\
\
\b 24. What is an object?
\b0 \
1. Object is a runtime entity that can contain state and behaviour and is an instance of a class\
1. state - contained in instance variables\
2. methods - contain behaviour\
2. new - keyword can be used for creation of object\
\
\b 25. What is the difference between an object-oriented programming language and object-based programming language?
\b0 \
1. Object oriented programming language:\
1. Supports all concepts of OOPs\
2. Doesn\'92t have in-built objects\
3. Example: Java, C#, Smalltalk, \'85\
2. Object based programming language:\
1. Might support only a subset of the concept of OOPs\
2. Has in-built objects - JavaScript: Window object\
3. Example: JavaScript, VBScript, \'85\
\
\b 26. What will be the initial value of an object reference which is defined as an instance variable?
\b0 \
1. null\
\
Constructor\
\b 27. What is the constructor?
\b0 \
1. A special method that is invoked during instantiation of an object\
1. overloaded constructors can be used to initialise state of an object\
2.
\b new
\b0 - calls default constructor by default\
3. It must not have explicit return type\
4. It\'92s name should be the same as the class name\
\
\b 28. How many types of constructors are used in Java?
\b0 \
1. Two types\
1. default constructor\
1. Does not accept any value\
2. Implicitly added if there is no constructors are defined\
2. parameterised constructor\
1. Initialises instance variables with given values\
\
\b 29. What is the purpose of a default constructor?
\b0 \
1. Added by compiler when no constructors are defined\
2. Default constructor calls superclass\'92s no argument constructor\
\
\b 30. Does constructor return any value?
\b0 \
1. No\
1. It is just called during instantiation\
\
\b 31. Is constructor inherited?
\b0 \
1. No - because it does not belong to the object\
\
\b 32. Can you make a constructor final?
\b0 \
1. No - it cannot be overridden => it cannot be modified => declaring it as
\b final
\b0 makes no sense\
\
\b 33. Can we overload the constructors?
\b0 \
1. Yes - By changing the number and type of arguments\
\
\b 34. What do you understand by copy constructor in Java?
\b0 \
1. There is no copy constructor in Java but one can copy values from one object to another like copy constructor in C++\
2. Ways to copy:\
1. Using constructor\
2. Assigning values of one object to another\
3.
\b clone()
\b0 method of
\b Object
\b0 class\
\
class Test \{\
int x, y;\
\}\
\
class Test2 implements Cloneable \{\
int a;\
int b;\
Test c = new Test();\
\
public Object clone() throws CloneNotSupportedException \{\
return super.clone();\
\}\
\}\
\
\b 35. What are the differences between the constructors and methods?
\b0 \
1. Constructor\
1. Used to initialise state of an object\
2. Must not have a return type\
3. Invoked implicitly\
4. Default constructor is provided if we don\'92t explicitly provide a constructor\
5. Name must be same as that of the class\
2. Method\
1. Used to expose behaviour of an object\
2. Must have a return type (void or others)\
3. Invoked explicitly\
4. Method is provided by the user (or library like Lombok)\
5. Name may or may not be the same as that of the class\
\
\b 36. What is the output of the following Java program?
\b0 \
\
public class Test \{\
Test(int a, int b) \{\
System.out.println(\'93a = \'93 + a + \'93 b = \'93 + b);\
\}\
\
Test(int a, float b) \{\
System.out.println(\'93a = \'93 + a + \'93 b = \'93 + b);\
\}\
\
public static void main(String args[]) \{\
byte a = 10;\
byte b = 15;\
Test test = new Test(a, b);\
\}\
\}\
\
a = 10 b = 15\
\
1. byte gets promoted to int\
\
\b 37. What is the output of the following Java program?
\b0 \
\
class Test \{\
int i;\
\}\
\
public class Main \{\
public static void main(String[] args) \{\
Test test = new Test();\
System.out.println(test.i);\
\}\
\}\
\
0 - initialized to default value when object is created (because default constructor is invoked which does not set any values)\
\
\b 38. What is the output of the following Java program?
\b0 \
\
class Test \{\
int test_a, test_b;\
\
Test (int a, int b) \{\
test_a = a;\
test_b = b;\
\}\
\
public static void main(String args[]) \{\
Test test = new Test();\
System.out.println(test.test_a + \'93 \'93 + test.test_b);\
\}\
\}\
\
1. Compiler error - No args constructor is not provided by the compiler when parameterised constructor(s) is/are defined by the user\
1. Fix: no args constructor must be explicitly defined for the class\
\
\b 39. What is static variable?
\b0 \
1. Class variable that can be referenced without an object\
2. Memory is allocated only once in the class area when the class is loaded\
\
\b 40. What is static method?
\b0 \
1. It a method that belongs to the class and doesn\'92t need an object to invoke it\
2. It can access and change values of a static variable\
\
\b 41. What are the restrictions that are applied to the Java static methods?
\b0 \
1. Cannot use non-static variables or call non-static methods directly\
2.
\b this
\b0 and
\b super
\b0 cannot be used (because they are non-static)\
\
\b 42. Why is the main method static?
\b0 \
1. It is convention used for JVM design\
1. No new object needs to be created to invoke main\
2. It is called
\b main
\b0 \
3. No default constructor or any constructor is invoked\
\
\b 43. Can we override the static methods?
\b0 \
\
\f1\b\fs30 \cf2 \cb3 \expnd0\expndtw0\kerning0
Overriding:
\b0 \'a0Overriding in Java simply means that the particular method would be \
called based on the run time type of the object and not on the compile time type of it \
(which is the case with overriden static methods)\
\b Hiding:
\b0 \'a0Parent class methods that are static are not part of a child class (although \
they are accessible), so there is no question of overriding it. Even if you add \
another static method in a subclass, identical to the one in its parent class, this \
subclass static method is unique and distinct from the static method in its parent \
class.
\f0\fs24 \cf0 \cb1 \kerning1\expnd0\expndtw0 \
\
\b 44. What is the static block?
\b0 \
1. It is used to initialise static data members \
2. It is executed before the main method (at the time of classloading)\
\
class A2 \{\
static \{\
System.out.println(\'93static block is invoked\'94);\
\}\
\
public static void main(String[] args) \{\
System.out.println(\'93Hello man\'94);\
\}\
\}\
\
\b 45. Can we execute a program without main() method?
\b0 \
1. Yes (but throws an error saying - there is not main method in class)\
1. Use static block\
\
\b 46. What if the static modifier is removed from the signature of the main method?
\b0 \
1. Throws error saying: main method is not declared as
\b public static
\b0 \
\
\b 47. What is the difference between static (class) method and instance method?
\b0 \
1. static (class) method\
1. method declared as
\b static\
\b0 2. They don\'92t need objects to call\
3. non-static members cannot be accessed in static method (static context)\
2. instance method\
1. method declared without
\b static
\b0 keyword (that is not a constructor)\
2. Object is required to call an instance method\
3. static and non-static members can be accessed in instance methods\
\
\b 48. Can we make constructors static?
\b0 \
1. constructors are not explicitly invoked but only when object is created hence no sense making it static\
1. Compiler error is thrown\
\
\b 49. Can we make the abstract methods static in Java?
\b0 \
1. Compiler throws an error: Illegal combination of modifiers abstract and static\
1. If we make a method static, it belongs to the class and it can be called directly (without overriding which is not allowed for static methods). But the method does not have any body and hence calling it is useless.\
\
\b 50. Can we declare the static variables and methods in an abstract class?
\b0 \
1. Yes. Only class name can be used to access static variables and methods and hence does not required extending the class to access them.\
\
\b 51. What is this keyword in Java?
\b0 \
1.
\b this
\b0 is a reference variable that refers to the current object\
1. It can be used to refer to instance variables and methods\
2. It can be passed as argument into methods or constructors\
3. It can be returned\
\
\b 52. What are the main uses of this keyword?
\b0 \
1.
\b this
\b0 can be used to refer to current class instance variable\
2.
\b this
\b0 can be used to invoke current class method (implicitly)\
3.
\b this()
\b0 calls current class constructor\
4.
\b this
\b0 can be passed as an argument in a method call\
5.
\b this
\b0 can be passed as an argument in constructor call\
6.
\b this
\b0 can be used to return the current class instance from method\
\
\b 53. Can we assign a reference to this variable?
\b0 \
1.
\b this
\b0 is a final variable (cannot be re-assigned any other reference)\
1. It points only to the current class instance and cannot be changed\
\
\b 54. Can this keyword be used to refer static members?
\b0 \
1. Yes\
1. object reference can be used to refer to static members (not recommended)\
\
\b 55. How can constructor chaining be done using this keyword?
\b0 \
\
public class Employee \{\
int id, age;\
String name, address;\
\
public Employee (int id) \{\
this.id = id;\
\}\
\
public Employee (int id, int age) \{\
this(id);\
this.age = age;\
\}\
\
public Employee(int id, int age, String name, String address) \{\
this(id, age);\
this.name = name;\
this.address = address;\
\}\
\
public String toString() \{\
return \'93id: \'94 + id + \'93, age: \'93 + age + \'93, name: \'93 + name + \'93, address: \'93 + address;\
\}\
\
public static void main(String[] args) \{\
Employee employee = new Employee(3, 45, \'93Folk\'94, \'93Drive\'94);\
System.out.println(employee);\
\}\
\}\
\
\b 56. What are the advantages of passing this into a method instead of the current class object itself?
\b0 \
1.
\b this
\b0 and current class object refer to the same object but\
1.
\b this
\b0 is final and cannot be changed (but non final class object reference can be re-assigned)\
2.
\b this
\b0 is allowed to be used in synchronised block\
\
synchronised (this) \{\
// critical section\
\}\
\
\b 57. What is the inheritance?
\b0 \
1. It is a mechanism that allows a class to obtain state and/or behaviour of another class (re-usability)\
1. Also allows overriding only behaviour that is different\
2. New classes can be built upon existing classes\
2. It is
\b is-a
\b0 relationship (parent-child)\
3. 5 types of inheritance in Java\
1. single-level\
2. multi-level\
3. multiple (not supported in Java)\
4. hierarchical (one class inherited by multiple sub-classes)\
5. hybrid (combo of single and multiple inheritance)\
\
\b 58. Why is the inheritance used in Java?
\b0 \
1. Code re-usability - derived classes do not need to re-define common methods in the base class\
2. Runtime polymorphism - achieved using inheritance\
3. Inheritance can be used to provide data hiding mechanism - certain private variables do not have to be given access\
\
\b 59. Which class is the superclass for all the classes?
\b0 \
1.
\b Object
\b0 \
\
\b 60. Why is multiple inheritance not supported in Java?
\b0 \
1. https://stackoverflow.com/questions/2515477/why-is-there-no-multiple-inheritance-in-java-but-implementing-multiple-interfac#:~:text=Java%20does%20not%20support%20multiple,explicitly%20or%20not%20at%20all.\
1. Two classes may defined different ways of doing the same thing and subclass can\'92t choose which one to pick\
2. Keeps the language simple (avoids diamond problem)\
3. The feature is rarely used (not worth having this feature at the cost of simplicity)\
4. Can be achieved using other safer mechanisms if need be (without sacrificing simplicity)\
\
\b 61. What is aggregation?
\b0 \
1. It is relationship between two classes\
1. aggregate class: contains reference to a class it owns\
1.
\b has-a
\b0 relationship\
2. Example:
\b Employee
\b0 has a field called
\b Address
\b0 where\
1.
\b Address
\b0 contains Address-line-1, City, State, pin-code\
\
\b 62. What is composition?
\b0 \
1. Composition: Holding reference of a class within another class\
1. If the containing class object cannot exist without the container class object (Special case of aggregation)\
1. Class contains Students\
1. Student cannot exist without a Class\
\
\b 63. What is the difference between aggregation and composition?
\b0 \
1. Aggregation - represents weak relationship (bike with mirror)\
2. Composition - represents strong relationship (bike with engine)\
\
\b 64. Why does Java not support pointers?
\b0 \
1. To improve security\
1. Pointers are unsafe\
1. Can lead to memory leaks\
2. Can lead to memory corruption\
3. Can lead to access of invalid memory locations\
1. Un-initialized or out of bounds memory access\
2. No direct access to memory and it is managed by JVM\
3. Not safe to expose memory through pointers on the internet (potential leak of information)\
2. Java uses reference model (but it has address to the actual object)\
1. Indirection is required for GC to work\
1. If GC does heap compaction, then certain objects are moved around in memory and the references are updated internally\
\
\b 65. What is super in Java?
\b0 \
1. It has the reference to immediate parent class object of an object\
1. Uses:\
1. Accessing variables of super-class object\
\
/* Base class vehicle */\
class Vehicle \{\
int maxSpeed = 120;\
\}\
\
/* sub class Car extending vehicle */\
class Car extends Vehicle \{\
int maxSpeed = 180;\
\
void display() \{\
/* print maxSpeed of base class (vehicle) */\
System.out.println(\'93Maximum Speed: \'93 + super.maxSpeed);\
\}\
\}\
\
/* Driver program to test */\
class Test \{\
public static void main(String[] args) \{\
Car small = new Car();\
small.display();\
\}\
\}\
\
2. Accessing methods of super-class object\
\
/* Base class Person */\
class Person \{\
void message() \{\
System.out.println(\'93This is person class\'94);\
\}\
\}\
\
/* Subclass Student */\
class Student extends Person \{\
void message() \{\
System.out.println(\'93This is student class\'94);\
\}\
\
// Note that display() is only in student class\
void display() \{\
// will invoke or call current class message() method\
message();\
\
// will invoke or call parent class message() method\
super.message();\
\}\
\}\
\
/* Driver program to test */\
class Test \{\
public static void main(String[] args) \{\
Student s = new Student();\
\
// calling display() of Student\
s.display();\
\}\
\}\
\
3. Accessing constructors of superclass (both non-parametric and parametric)\
\
/* superclass Person */\
class Person \{\
Person() \{\
System.out.println(\'93Person class Constructor\'94);\
\}\
\}\
\
/* subclass Student extending the Person class */\
class Student extends Person \{\
Student() \{\
// invoke or call parent class constructor\
super();\
\
System.out.println(\'93Student class Constructor\'94);\
\}\
\}\
\
/* Driver program to test */\
class Test \{\
public static void main(String[] args) \{\
Student s = new Student();\