├── Lecture 01 ├── Question1.java ├── Question2.java ├── Question3.java └── Question4.java ├── Lecture 02 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 03 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java └── Question12.java ├── Lecture 04 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java ├── Question19.java └── Question20.java ├── Lecture 05 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java └── Question10.java ├── Lecture 06 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java └── Question11.java ├── Lecture 07 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java └── Question14.java ├── Lecture 08 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java └── Question12.java ├── Lecture 09 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 10 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 11 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 12 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 13 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 14 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 15 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java └── Question19.java ├── Lecture 16 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java └── Question8.java ├── Lecture 17 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java ├── Question19.java └── Question20.java ├── Lecture 18 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java └── Question7.java ├── Lecture 19 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java ├── Question8.java └── Question9.java ├── Lecture 20 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java └── Question7.java ├── Lecture 21 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java ├── Question19.java ├── Question20.java ├── Question21.java ├── Question22.java ├── Question23.java ├── Question24.java ├── Question25.java ├── Question26.java ├── Question27.java └── Question28.java ├── Lecture 22 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java └── Question12.java ├── Lecture 23 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java └── Question8.java ├── Lecture 24 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java └── Question5.java ├── Lecture 25 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java └── Question15.java ├── Lecture 26 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java ├── Question8.java └── Question9.java ├── Lecture 27 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java └── Question7.java ├── Lecture 28 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java └── Question12.java ├── Lecture 29 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java └── Question14.java ├── Lecture 30 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java └── Question5.java ├── Lecture 31 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java ├── Question19.java ├── Question20.java ├── Question21.java ├── Question22.java ├── Question23.java ├── Question24.java ├── Question25.java └── Question26.java ├── Lecture 32 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 33 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java └── Question5.java ├── Lecture 34 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java └── Question14.java ├── Lecture 35 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 36 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 37 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java ├── Question8.java └── Question9.java ├── Lecture 38 ├── Question1.java └── Question2.java ├── Lecture 39 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 40 ├── Question1.java └── Question2.java ├── Lecture 41 ├── Question1.java ├── Question2.java ├── Question3.java └── Question4.java ├── Lecture 42 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 43 ├── Question1.java └── Question2.java ├── Lecture 44 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java └── Question15.java ├── Lecture 45 ├── Question1.java ├── Question2.java ├── Question3.java └── Question4.java ├── Lecture 46 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java └── Question11.java ├── Lecture 47 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java ├── Question7.java └── Question8.java ├── Lecture 48 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java ├── Question6.java └── Question7.java ├── Lecture 49 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java └── Question5.java ├── Lecture 50 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 51 ├── Question1.java ├── Question2.java ├── Question3.java └── Question4.java ├── Lecture 52 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java └── Question5.java ├── Lecture 53 ├── Question1.java └── Question2.java ├── Lecture 54 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 55 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 56 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 57 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java └── Question19.java ├── Lecture 58 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java ├── Question11.java ├── Question12.java ├── Question13.java ├── Question14.java ├── Question15.java ├── Question16.java ├── Question17.java ├── Question18.java ├── Question19.java └── Question20.java ├── Lecture 59 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java ├── Question08.java ├── Question09.java ├── Question10.java └── Question11.java ├── Lecture 60 ├── Question1.java ├── Question2.java ├── Question3.java └── Question4.java ├── Lecture 61 ├── Question1.java ├── Question2.java ├── Question3.java ├── Question4.java ├── Question5.java └── Question6.java ├── Lecture 62 ├── Question1.java ├── Question2.java └── Question3.java ├── Lecture 65 ├── Question01.java ├── Question02.java ├── Question03.java ├── Question04.java ├── Question05.java ├── Question06.java ├── Question07.java └── Question08.java ├── README.md └── sciptt.py /Lecture 01/Question2.java: -------------------------------------------------------------------------------- 1 | public class FirstProgram { 2 | public static void main(String[] args) { 3 | System.out.println("Hello Java"); 4 | } 5 | } -------------------------------------------------------------------------------- /Lecture 01/Question4.java: -------------------------------------------------------------------------------- 1 | package q10734; 2 | // A sample program 3 | public class FirstJavaProgram { 4 | public static void main(String[] args) { 5 | System.out.println("India got its independence in 1947"); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 02/Question1.java: -------------------------------------------------------------------------------- 1 | Click on the below Live Demo button to learn the steps involved in writing and executing a Java program. 2 | 3 | 4 | 5 | After learning from the Live Demo, select all correct statements given below: 6 | 7 | Answer 8 | 9 | The Java compiler uses the Java source file and generates file with .class extension. 10 | The file with .class extension is called as Java class file. 11 | The Java class file contains the Java bytecode, which is executed by the Java Virtual Machine (JVM). -------------------------------------------------------------------------------- /Lecture 02/Question3.java: -------------------------------------------------------------------------------- 1 | package q10737; 2 | public class Student { 3 | 4 | } -------------------------------------------------------------------------------- /Lecture 02/Question4.java: -------------------------------------------------------------------------------- 1 | package q10751; 2 | public class Student { 3 | 4 | } -------------------------------------------------------------------------------- /Lecture 02/Question5.java: -------------------------------------------------------------------------------- 1 | package q10754; 2 | public class Student { 3 | 4 | } -------------------------------------------------------------------------------- /Lecture 02/Question6.java: -------------------------------------------------------------------------------- 1 | package q11602; 2 | public class Test { 3 | 4 | } -------------------------------------------------------------------------------- /Lecture 03/Question02.java: -------------------------------------------------------------------------------- 1 | package q10798; 2 | public class SpacesAndTabsDemo { 3 | public static void main(String[] args) { 4 | for (int i = 0; i < 5; i++) { 5 | System.out.println("i = " + i); 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /Lecture 03/Question03.java: -------------------------------------------------------------------------------- 1 | package q10799; 2 | /** 3 | * This is a sample Java Doc comment 4 | * @author James 5 | */ 6 | public class TestComments { 7 | public static void main(String[] args) { 8 | /* this is an example for standard comment which can span across multiple lines */ 9 | // this is an end of line comment 10 | System.out.println("This is a simple example on java comment lines"); 11 | } 12 | } -------------------------------------------------------------------------------- /Lecture 03/Question04.java: -------------------------------------------------------------------------------- 1 | package q10800; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 03/Question05.java: -------------------------------------------------------------------------------- 1 | package q10801; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | String text1 = "He"; 5 | String text2 = "llo"; 6 | String text3 = text1 + text2; 7 | String text4 = ", I am learning Java"; 8 | String text5 = text3 + text4; 9 | System.out.println("text5 = " + text5); 10 | } 11 | } -------------------------------------------------------------------------------- /Lecture 03/Question06.java: -------------------------------------------------------------------------------- 1 | package q10802; 2 | public class Test { 3 | public static void main(String[] args) { 4 | System.out.println("Hello"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 03/Question07.java: -------------------------------------------------------------------------------- 1 | package q10802; 2 | public class Test { 3 | public static void main(String[] args) { 4 | System.out.println("Hello"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 03/Question08.java: -------------------------------------------------------------------------------- 1 | package q10804; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 03/Question09.java: -------------------------------------------------------------------------------- 1 | package q10805; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 03/Question10.java: -------------------------------------------------------------------------------- 1 | package q10806; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | 6 | }} -------------------------------------------------------------------------------- /Lecture 03/Question11.java: -------------------------------------------------------------------------------- 1 | package q10807; 2 | public class PrintHello { 3 | public static void main(String[] args){ 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 03/Question12.java: -------------------------------------------------------------------------------- 1 | package q10808; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | String text1 = "He"; 5 | String text2 = "llo"; 6 | String text3 = text1 + text2; 7 | System.out.println("text3 = " + text3); 8 | } 9 | } -------------------------------------------------------------------------------- /Lecture 04/Question01.java: -------------------------------------------------------------------------------- 1 | package q10755; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question02.java: -------------------------------------------------------------------------------- 1 | package q10756; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question03.java: -------------------------------------------------------------------------------- 1 | package q10757; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question04.java: -------------------------------------------------------------------------------- 1 | package q10758; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question05.java: -------------------------------------------------------------------------------- 1 | package q10759; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question06.java: -------------------------------------------------------------------------------- 1 | package q10760; 2 | public class PrintHello { 3 | public static void main (String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question07.java: -------------------------------------------------------------------------------- 1 | package q10785; 2 | public class PrintHello { 3 | public static void main(String[] args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question08.java: -------------------------------------------------------------------------------- 1 | package q10790; 2 | public class PrintHello { 3 | public static void main(String []args) { 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question09.java: -------------------------------------------------------------------------------- 1 | package q10791; 2 | public class PrintHello { 3 | public static void main (String []args){ 4 | System.out.println("Hello, I am learning Java!"); 5 | } 6 | } -------------------------------------------------------------------------------- /Lecture 04/Question10.java: -------------------------------------------------------------------------------- 1 | package q10792; 2 | public class CommandLineArgumentsDemo { 3 | public static void main(String[] args) { 4 | System.out.println("args.length : " + args.length); 5 | System.out.println("args[0] : " + args[0]); 6 | System.out.println("args[1] : " + args[1]); 7 | System.out.println("args[2] : " + args[2]); 8 | System.out.println("args[3] : " + args[3]); 9 | } 10 | } -------------------------------------------------------------------------------- /Lecture 04/Question11.java: -------------------------------------------------------------------------------- 1 | package q10793; 2 | public class CommandLineArgumentDemo { 3 | public static void main(String[] args) { 4 | //Write the code fragment in the below println( ) method to print only the second argument 5 | System.out.println( args[1] ); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 04/Question12.java: -------------------------------------------------------------------------------- 1 | package q10794; 2 | public class CommandLineArgumentDemo { 3 | public static void main(String[] args) { 4 | //Write the code fragment in the below println( ) method to print only the fourth argument 5 | System.out.println( args[3] ); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 04/Question13.java: -------------------------------------------------------------------------------- 1 | package q10795; 2 | public class CommandLineArgumentDemo { 3 | public static void main(String[] args) { 4 | //Write the code fragment in the below println( ) method to print only the first argument 5 | System.out.println( args[0] ); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 04/Question14.java: -------------------------------------------------------------------------------- 1 | package q10796; 2 | public class CommandLineArgumentDemo { 3 | public static void main(String[] args) { 4 | //Write the code fragment in the below println( ) method to print only the fifth argument 5 | System.out.println( args[4] ); 6 | } 7 | } -------------------------------------------------------------------------------- /Lecture 04/Question15.java: -------------------------------------------------------------------------------- 1 | The PrintHello class demonstrates printing text to the console. 2 | 3 | You will notice that the line, System.out.println("Hello, I am learning Java!"); is responsible for printing the text. 4 | 5 | This line is written inside the main method. 6 | Please note that in Java, String literals (i.e. text) must be enclosed in double quotes. 7 | In our case you will notice that the String literal Hello, I am learning Java! is enclosed in double quotes. 8 | 9 | Click on to understand basics of printing. 10 | 11 | See the code and retype the same code. 12 | 13 | Note: Please don't change the package name. 14 | 15 | package q10761; 16 | public class PrintHello { 17 | public static void main(String[] args) { 18 | System.out.println("Hello, I am learning Java!"); 19 | } 20 | } -------------------------------------------------------------------------------- /Lecture 04/Question16.java: -------------------------------------------------------------------------------- 1 | Identify the error in the below code and correct it. 2 | 3 | Note: Please don't change the package name. 4 | 5 | ; 6 | 7 | ' 8 | 9 | package q10762; 10 | public class PrintHello { 11 | public static void main(String[] args) { 12 | System.out.println("Hello"); 13 | } 14 | } -------------------------------------------------------------------------------- /Lecture 04/Question17.java: -------------------------------------------------------------------------------- 1 | Find the error in the given code and correct it. 2 | 3 | Note: Please don't change the package name.' 4 | 5 | 6 | package q10763; 7 | public class PrintHello { 8 | public static void main(String[] args) { 9 | System.out.println("Hello"); 10 | } 11 | } -------------------------------------------------------------------------------- /Lecture 04/Question18.java: -------------------------------------------------------------------------------- 1 | Identify the error in the given code and correct it. 2 | 3 | Note: Please don't change the package name.' 4 | 5 | 6 | package q10764; 7 | public class PrintHello { 8 | public static void main(String[] args) { 9 | System.out.println("Hello"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Lecture 04/Question20.java: -------------------------------------------------------------------------------- 1 | In Java, a String is enclosed in double quotes and not in single quotes. We will learn more about Strings in the ensuing sections. Identify the error and correct the code. 2 | 3 | package q10766; 4 | public class PrintHello { 5 | public static void main(String[] args) { 6 | System.out.println("Hello, I am learning Java!"); 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Lecture 05/Question02.java: -------------------------------------------------------------------------------- 1 | The numbers that we use regularly in our daily life, like: 0, 1, 2, 3,...2333, 99999 etc belong to Decimal System. It is also called a base-10 system. 2 | 3 | It is called base-10 because it uses 10 unique digits from 0 to 9 to represent any number in this number system. 4 | 5 | A base (also called radix) is the number of unique digits or symbols (including 0) used to represent a given number. 6 | 7 | In Decimal System (which is base-10) a total of 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) are used to represent a number of any size (magnitude). 8 | 9 | For example, Three Hundred and Twenty One is represented as 321, where 10 | 321 = (3 * 102) + (2 * 101) + (1 * 100) 11 | 321 = (3 * 100) + (2 * 10) + (1 * 1) 12 | [100's] [10's] [units] 13 | Similarly fractions are represented with the base (10) being raised to a negative power. 14 | 15 | Select all the correct statements given below. 16 | 17 | Answer 18 | 19 | In Decimal System, 102 = (1 * 102) + (0 * 101) + (2 * 100) 20 | In Decimal System, 0 = (0 * 100) -------------------------------------------------------------------------------- /Lecture 05/Question05.java: -------------------------------------------------------------------------------- 1 | Select all the correct answers 2 | Answer 3 | 4 | In Binary System, decimal 3 is represented as 0b11 -------------------------------------------------------------------------------- /Lecture 06/Question02.java: -------------------------------------------------------------------------------- 1 | In Java, there are different numeric data types to represent whole numbers(integral) and fractions(floating-point) of different sizes (magnitudes). 2 | 3 | The numeric types are the integral types and the floating-point types. 4 | 5 | The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (#3.1). 6 | 7 | The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers. 8 | 9 | [Source: Java Language Specification, Primitive Types and Values] 10 | 11 | Select all the correct statements given below. 12 | 13 | 14 | Answer 15 | 16 | 17 | char values are 16-bit integers 18 | double can hold a value larger than the maximum float value 19 | long can hold a value greater than the maximum int value -------------------------------------------------------------------------------- /Lecture 06/Question03.java: -------------------------------------------------------------------------------- 1 | In Java, the int data type denotes a 32-bit signed (two's-complement) integer, which can hold any value between - 2,147,483,648 (-231) and 2,147,483,647 (231-1). 2 | 3 | The primitive type int has a corresponding wrapper class called Integer. Both int and Integer can be used interchangeably. Which means, we can say: 4 | int num1 = 3; 5 | Integer num2 = 4; 6 | Integer total = num1 + num2; 7 | int valueOfNum2 = total - num1; 8 | The fixed values 3, 4 assigned to num1 and num2 respectively are called integer literals. 9 | The default value of a primitive int is 0, when not initialized. However, the default value of a reference of type Integer is null, when not initialized. 10 | In Java 8 and later, int can also represent unsigned 32-bit integers, with a min value of 0 and a max value of 232-1. To use unsigned integers, use Integer class. 11 | We will learn more about the wrapper class Integer later. 12 | 13 | Select all the correct statements given below. 14 | 15 | Answer 16 | 17 | An int literal value can be a negative or a positive number with the range of -231 and 231-1. -------------------------------------------------------------------------------- /Lecture 06/Question04.java: -------------------------------------------------------------------------------- 1 | package q10770; 2 | public class IntegerDemo { 3 | public static void main(String args[]) { 4 | int decimalOne = 1; 5 | System.out.println("decimalOne = " + decimalOne); 6 | int binaryThree = 0b11; 7 | System.out.println("binaryThree = " + binaryThree); 8 | int octalEight = 010; 9 | System.out.println("octalEight = " + octalEight); 10 | int hexTen = 0xA; 11 | System.out.println("hexTen = " + hexTen); 12 | int unicodeValueOfOne = '1'; 13 | System.out.println("unicodeValueOfOne = " + unicodeValueOfOne); 14 | int unicodeValueOfA = 'A'; 15 | System.out.println("unicodeValueOfA = " + unicodeValueOfA); 16 | int unicodeValueOfZ = 'Z'; 17 | System.out.println("unicodeValueOfZ = " + unicodeValueOfZ); 18 | } 19 | } -------------------------------------------------------------------------------- /Lecture 06/Question05.java: -------------------------------------------------------------------------------- 1 | package q10771; 2 | public class CalculateDifference { 3 | public static void main(String[] args) { 4 | // In the below two lines write code to convert 5 | // the string value passed in args[0] and args[1] into int values using Integer.parseInt method 6 | // convert the value in args[0] to int and store in firstValue 7 | // convert the value in args[1] to int and store in secondValue 8 | int firstValue = Integer.parseInt(args[0]); 9 | int secondValue =Integer.parseInt(args[1]); 10 | System.out.println("The difference of args[0] and args[1] is : " + (firstValue - secondValue)); 11 | } 12 | } -------------------------------------------------------------------------------- /Lecture 06/Question08.java: -------------------------------------------------------------------------------- 1 | In Java, the short data type denotes a 16-bit signed (two's-complement) integer, which can hold any value between - 32,768 (-215) and 32,767 (215-1). 2 | 3 | The primitive type short has a corresponding wrapper class called Short. Both short and Short can be used interchangeably. Which means, we can say: 4 | short num1 = 3; 5 | Short num2 = 4; 6 | Short total = (short)(num1 + num2); 7 | short valueOfNum2 = (short)(total - num1); 8 | The default value of a primitive short is 0, when not initialized. However, the default value of a reference of type Short is null, when not initialized. 9 | We normally use int instead of short to hold integer values. However, when using large arrays if we know that the values stored will be within the range of a short, using an array of type short may help saving memory. 10 | 11 | We will learn more about the wrapper class Short later. 12 | 13 | Select all the correct statements given below. 14 | 15 | Answer 16 | 17 | A short literal value can be a negative or a positive number with the range of -215 and 215-1. -------------------------------------------------------------------------------- /Lecture 06/Question09.java: -------------------------------------------------------------------------------- 1 | package q10774; 2 | public class ShortDemo { 3 | public static void main(String[] args) { 4 | short mangos = 3; 5 | short bananas = 4; 6 | short fruits =(short) (mangos + bananas); 7 | System.out.println("fruits = " + fruits);// should print 7 8 | } 9 | } -------------------------------------------------------------------------------- /Lecture 06/Question10.java: -------------------------------------------------------------------------------- 1 | package q10775; 2 | public class GuessShort { 3 | public static void main(String[] args) { 4 | //in the below line write code to convert 5 | //the string value passed in args[0] into a short 6 | //Hint: Since args[0] is going to be a String 7 | // you can use Short.parseShort(args[0]); to convert 8 | // the value in args[0] into a short 9 | short shortValue =Short.parseShort(args[0]) ; 10 | 11 | if (Byte.MIN_VALUE <= shortValue && shortValue <= Byte.MAX_VALUE) { 12 | System.out.println("The short value " + shortValue + " is in the range of a byte "); 13 | } else { 14 | System.out.println("The short value in args[0] is : " + shortValue); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Lecture 06/Question11.java: -------------------------------------------------------------------------------- 1 | package q10776; 2 | public class HolidaysCalculator { 3 | public static void main(String[] args) { 4 | short nationalHolidays =Short.parseShort(args[0]) ; 5 | short companyHolidays =Short.parseShort(args[1]) ; 6 | short totalHolidays = (short)(nationalHolidays + companyHolidays); 7 | System.out.println("totalHolidays = " + totalHolidays); 8 | } 9 | } -------------------------------------------------------------------------------- /Lecture 07/Question01.java: -------------------------------------------------------------------------------- 1 | In Java, the byte data type denotes a 8-bit signed (two's-complement) integer, which can hold any value between - 128 (-27) and 127 (27-1). 2 | 3 | The primitive type byte has a corresponding wrapper class called Byte. Both byte and Byte can be used interchangeably. Which means, we can say: 4 | byte num1 = 3; 5 | Byte num2 = 4; 6 | Byte total = (byte)(num1 + num2); 7 | byte valueOfNum2 = (byte)(total - num1); 8 | The default value of a primitive byte is 0, when not initialized. However, the default value of a reference of type Byte is null, when not initialized. 9 | We normally use int instead of byte to hold integer values. However (like in the case of short), when using large arrays if we know that the values stored will be within the range of a byte, using a byte array may help saving memory. 10 | A byte data type can also be used to indicate to a programmer that the value it will hold is small. More commonly they are used while manipulating raw data using byte arrays. 11 | 12 | We will learn more about the wrapper class Byte later. 13 | 14 | Select all the correct statements given below. 15 | 16 | 17 | 18 | Answer 19 | 20 | The max value a byte can be assigned is 0b1111111 -------------------------------------------------------------------------------- /Lecture 07/Question04.java: -------------------------------------------------------------------------------- 1 | Create a class HolidaysCalculator with a main method. The method receives two command line arguments the first argument contains the count of national holidays declared in that country and the second argument contains a count of company specific holidays. Write a code to print the total number of holidays as the sum of both national and company holidays. 2 | 3 | For example: 4 | Cmd Args : 3 4 5 | totalHolidays = 7 6 | 7 | 8 | Note: Please don't change the package name. 9 | 10 | 11 | 12 | Answer 13 | 14 | package q10780; 15 | public class HolidaysCalculator { 16 | public static void main(String[] args) { 17 | byte nationalHolidays = Byte.parseByte(args[0]); 18 | byte companyHolidays = Byte.parseByte(args[1]); 19 | byte totalHolidays =(byte)(nationalHolidays + companyHolidays); 20 | System.out.println("totalHolidays = " + totalHolidays); 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture 07/Question08.java: -------------------------------------------------------------------------------- 1 | The Double class wraps a primitive double value. 2 | This class provides constants and methods that are useful while working with large precision fractional numbers. For example it has a method Double.parseDouble(String s) which can be used to convert a string value to a double 3 | Double y = Double.parseDouble("380.234d"); 4 | //parses the string literal "380.234d" 5 | In the below code, the main method of CalculateDifference will be passed two arguments. Both the values can be any number between Double.MIN_VALUE to Double.MAX_VALUE. 6 | 7 | Complete the below code so that it produces the correct output. 8 | 9 | 10 | Note: Please don't change the package name. 11 | 12 | Answer 13 | 14 | package q10857; 15 | public class CalculateDifference { 16 | public static void main(String[] args) { 17 | double firstValue =Double.parseDouble(args[0]) ; 18 | double secondValue =Double.parseDouble(args[1]) ; 19 | 20 | System.out.println("The difference of args[0] and args[1] is : " + (firstValue - secondValue)); 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture 07/Question09.java: -------------------------------------------------------------------------------- 1 | Select all the correct statements. 2 | 3 | 4 | Answer 5 | 6 | 7 | double d = 7.; is a valid statement 8 | double d = .7; is a valid statement 9 | double d = 7; is a valid statement 10 | double d = 1e1; is a valid statement -------------------------------------------------------------------------------- /Lecture 07/Question11.java: -------------------------------------------------------------------------------- 1 | Retype the below code and submit it to see the result. 2 | If you expect it to print 0.3, there is a nasty surprise for you!! It actually evaluates to 0.30000000000000004 3 | 4 | To understand what is wrong, you should know more about how floating point numbers are represented in binary format(base-2). The number of bits used for precision limits the set of rational numbers that can be represented exactly using a float or a double. This is why we get results like these when we use float or double in arithmetic operations. 5 | 6 | Note: Never use float or double in currency calculations which require a high degree of precision. 7 | BigDecimal is the right choice in Java for such calculations. 8 | We will learn about BigDecimal in the next sections. 9 | 10 | Note: Please don't change the package name. 11 | 12 | Answer 13 | 14 | package q10859; 15 | public class FloatingPointArithmetic { 16 | public static void main(String[] args) { 17 | double x = 0.2; 18 | double y = 0.1; 19 | System.out.print(x + y); 20 | } 21 | } -------------------------------------------------------------------------------- /Lecture 07/Question12.java: -------------------------------------------------------------------------------- 1 | Java provides a class called BigDecimal for representing and computing decimal numbers with any required precision. 2 | 3 | BigDecimal is not a wrapper class of any primitive data type. It is a convenience class for handling large decimal numbers with arbitrary precision. The value to be represented by BigDecimal should be passed to one of the BigDecimal constructors. 4 | 5 | Retype the code below. 6 | 7 | You will notice an import statement at the beginning of the program to use BigDecimal class. We will learn about packages and import statements in other sections. 8 | 9 | Note: Please don't change the package name. 10 | 11 | 12 | package q10860; 13 | import java.math.BigDecimal; 14 | public class BigDecimalUsage { 15 | public static void main(String[] args) { 16 | BigDecimal x = new BigDecimal("3.14159265358"); 17 | System.out.print(x); 18 | } 19 | } -------------------------------------------------------------------------------- /Lecture 08/Question01.java: -------------------------------------------------------------------------------- 1 | Java version 7 has added a new feature to improve the readability of Numeric Literals. 2 | We can include an underscore ('_') in numeric literals to show grouping of digits. 3 | For example: 4 | long mySalary = 900_000L;// Which is equal to 900000 (Nine Hundred Thousand) 5 | long magicNumberInHex = 0xCAFE_BABE; // Want to know more about Magic Number? 6 | 7 | Note: Underscore is only for programmer's readability, it is completely ignored during computations. 8 | 9 | Usage rules for underscore: 10 | Underscore can only be used between two digits. 11 | Underscore cannot be used at the beginning or ending of a number. 12 | Underscore cannot be used before a suffix like F (used in float), L (used in Long) or D (used in Double). 13 | Underscore cannot be used before or between a radix prefix used for binary or octal or hex numbers 14 | 15 | Select all the correct answers given bellow' 16 | 17 | 18 | Answer 19 | 20 | int mySelf = 0___0___7; -------------------------------------------------------------------------------- /Lecture 08/Question02.java: -------------------------------------------------------------------------------- 1 | Select all the correct statements from the given options. 2 | 3 | Answer 4 | 5 | int x = 07_08_09; is a valid usage of Underscore 6 | int x = 0xA_B; 7 | int x = 1_____________0; -------------------------------------------------------------------------------- /Lecture 08/Question03.java: -------------------------------------------------------------------------------- 1 | In Java, the boolean data type denotes a logical quantity with two possible values, indicated by the literals true or false. 2 | 3 | The primitive type boolean has a corresponding wrapper class called Boolean. Both booleanBoolean can be used interchangeably. Which means, we can say: 4 | Boolean isHot = true; 5 | boolean isSnowing = false; 6 | boolean isSummer = isHot && (!isSnowing); 7 | The fixed values true and false used above are called boolean literals. 8 | The default value of a primitive boolean is false, when not initialized. However, the default value of a reference of type Boolean is null, when not initialized. 9 | We will learn more about the wrapper class Boolean later. 10 | 11 | Select all the correct statements given below. 12 | 13 | Answer 14 | 15 | boolean isResponse = false; is a valid statement. -------------------------------------------------------------------------------- /Lecture 08/Question05.java: -------------------------------------------------------------------------------- 1 | In Java, the char data type denotes a 16-bit unsigned integer (between 0 and 65535), which represent the Unicode values between '\u0000' and '\uffff' . 2 | 3 | The primitive type char has a corresponding wrapper class called Character. Both char and Character can be used interchangeably. Which means, we can say: 4 | char gender1 = 'M'; 5 | Character gender2 = gender1; 6 | A character literal must be wrapped in single quotes and it cannot span multiple lines. 7 | 8 | The fixed value 'M' assigned to gender1 is called character literal. 9 | The default value of a primitive char is 0, when not initialized. However, the default value of a reference of type Character is null, when not initialized. 10 | 11 | Since char is a 16-bit integer type, it can be interchangeably used with int. 12 | We will learn more about the wrapper class Character later. 13 | 14 | Select all the correct statements given below. 15 | 16 | Answer 17 | 18 | A char literal value can be a number between 0 and 216-1. -------------------------------------------------------------------------------- /Lecture 08/Question06.java: -------------------------------------------------------------------------------- 1 | Understanding special characters 2 | 3 | Answer 4 | 5 | package q10812; 6 | public class SpecialCharacters { 7 | public static void main(String[] args) { 8 | char aChar = 'A'; 9 | char tabChar = '\t'; 10 | char digitChar = '7'; 11 | char newLineChar = '\n'; 12 | char spaceChar = ' '; 13 | char hyphenChar = '-'; 14 | char percentageChar = '%'; 15 | System.out.print(aChar); 16 | System.out.print(tabChar); 17 | System.out.print(digitChar); 18 | System.out.print(newLineChar); 19 | System.out.print(spaceChar); 20 | System.out.print(hyphenChar); 21 | System.out.print(percentageChar); 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 08/Question10.java: -------------------------------------------------------------------------------- 1 | A String is a sequence of zero or more characters enclosed in double-quotes. For example, 2 | String name = "Ganga"; 3 | The value "Ganga" is called the string literal. We cannot open a double-quote on one line and close the double-quote in another line, they both should be in the same line. 4 | 5 | We can embed escape characters (like \t, \n) also in a string literal. For example, 6 | String greetingText = "Hello Thames\nWelcome to London!"; 7 | The above line when printed would have "Hello Thames" in one line and "Welcome to London!" in the next line. 8 | 9 | We will learn about the String class in detail later. 10 | 11 | Select all correct statements. 12 | 13 | 14 | 15 | Answer 16 | 17 | 18 | String text = "Hello" 19 | + "World"; 20 | is a valid statement. -------------------------------------------------------------------------------- /Lecture 09/Question2.java: -------------------------------------------------------------------------------- 1 | In the below code 2 | double x = 2.3d; 3 | String name = "Bhargo"; 4 | byte[] buffer = new byte[1024]; 5 | x is called a variable 6 | name is called a reference 7 | buffer is called a reference 8 | A variable can be compared to a cup, which holds the value of the primitive date type. 9 | A reference can be compared to a cup, which holds the address of the object it points to. 10 | 11 | When not initialized, the default value of a variable is 0 for numeric primitive types and false for boolean types. 12 | When not initialized, the default value of a reference is null. If a reference has a null value, it means that the reference does not refers (points) to any object. 13 | Select all the correct statements that apply to the below code. 14 | String name = null; 15 | int age = null; 16 | byte[] buffer = null; 17 | boolean status = null; 18 | 19 | 20 | Answer 21 | 22 | 23 | Statement String name = null; is correct 24 | Statement byte[] buffer = null; is correct -------------------------------------------------------------------------------- /Lecture 09/Question3.java: -------------------------------------------------------------------------------- 1 | In every programming language there are certain keywords. Below are the keywords in Java: 2 | abstract continue for new switch 3 | assert default if package synchronized 4 | boolean do goto private this 5 | break double implements protected throw 6 | byte else import public throws 7 | case enum instanceof return transient 8 | catch extends int short try 9 | char final interface static void 10 | class finally long strictfp volatile 11 | const float native super while 12 | These words have special meaning and usage. They cannot be used for naming variables, references or methods or classes. 13 | 14 | null is not a keyword in Java. 15 | if is a keyword -------------------------------------------------------------------------------- /Lecture 09/Question4.java: -------------------------------------------------------------------------------- 1 | The Float.NaN and Double.NaN refer to a result that is not a number. 2 | For example we get a NaN (not a number result) when dividing zero by zero or when we try to calculate the square root of a negative number. 3 | 4 | While coding in Java, to verify if the result of an expression is not a number, we should never compare the result (using == operator) to either Float.NaN or Double.NaN. 5 | 6 | The correct way of verifying if the value is not a number is to use the method Float.isNaN() or Double.isNaN(). 7 | 8 | See and retype the below code, which shows the correct usage of isNaN() method. 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10823; 13 | public class CorrectUsageOfNaN { 14 | public static void main(String[] args) { 15 | double x = 0d / 0d; 16 | System.out.println("x == Double.NaN : " + (x == Double.NaN)); 17 | System.out.println("Double.isNaN(x) : " + Double.isNaN(x)); 18 | } 19 | } -------------------------------------------------------------------------------- /Lecture 09/Question5.java: -------------------------------------------------------------------------------- 1 | In Java, null is a special type. It has only one value and that is null. 2 | 3 | The null literal is written as plain null, without any double quotes. 4 | 5 | A reference having a null value indicates that it is not pointing to a valid object. 6 | 7 | In Java code, when an attempt is made to access a member (like field or method using the dot ) of a null reference, NullPointerException is thrown. We will learn more about Exceptions later. 8 | 9 | Float a; // a is initalized to null, it is equal to saying Float a = null; 10 | String text; // text is initalized to null, it is equal to saying String text = null; 11 | int x; // x is initialized to 0See and retype the code given below. 12 | 13 | Note: Please don't change the package name.' 14 | 15 | package q10824; 16 | public class NullTest { 17 | public static void main(String[] args) { 18 | String text = null; 19 | System.out.println("text : " + text); 20 | byte[] buffer = new byte[1024]; 21 | buffer = null; 22 | System.out.println("buffer : " + buffer); 23 | String name = null; 24 | System.out.println("text == name : " + (text == name)); 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 10/Question3.java: -------------------------------------------------------------------------------- 1 | package q10831; 2 | public class RelationalOperatorDemo { 3 | public static void main(String[] args) { 4 | int x = 3; 5 | int y = 4; 6 | if (x != y) { 7 | System.out.println("x != y : " + (x != y)); 8 | } 9 | if (x < y) { 10 | System.out.println("x < y : " + (x < y)); 11 | } 12 | x++;// incrementing x 13 | if (x == y) { 14 | System.out.println("x == y : " + (x == y)); 15 | } 16 | --y;// decrementing y 17 | if (x > y) { 18 | System.out.println("x > y : " + (x > y)); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Lecture 11/Question1.java: -------------------------------------------------------------------------------- 1 | In Java, we have 5 Unary operators: 2 | Operator Description 3 | + Unary Plus, used for indicating a positive value 4 | - Unary Minus, subtracts from zero. 5 | ++ Increment operator. Increments the value by one. 6 | -- Decrement operator. Decrements the value by one. 7 | ! Negation operator. Negates a boolean value. 8 | 9 | The usage is 10 | operator operand 11 | 12 | According to Java coding conventions, no space should be provided between a unary operator and operand. 13 | 14 | Note: When Increment or Decrement operator is applied before an operand (e.g. ++i or --i), the value of the operand is changed first and the changed value is substituted in the expression. 15 | When Increment or Decrement operator is applied after an operand (e.g. i++ or i--), the original value of the operand is substituted in the expression first and the value is changed later. 16 | 17 | Select all the correct statements for the below code: 18 | int x = 10; 19 | int y = -x; 20 | int z = x++; 21 | int k = ++x; 22 | 23 | 24 | Answer 25 | 26 | 27 | Value of y is -10 28 | Value of k is 12 -------------------------------------------------------------------------------- /Lecture 11/Question2.java: -------------------------------------------------------------------------------- 1 | Understanding Unary Operators 2 | 3 | package q10829; 4 | public class UnaryOperatorsDemo { 5 | public static void main(String[] args) { 6 | int x = 16; 7 | System.out.println("+x = " + (+x)); 8 | System.out.println("-x = " + (-x)); 9 | System.out.println("x = " + x); 10 | System.out.println("++x = " + (++x)); 11 | System.out.println("x = " + x); 12 | System.out.println("x++ = " + (x++)); 13 | System.out.println("x = " + x); 14 | System.out.println("--x = " + (--x)); 15 | System.out.println("x = " + x); 16 | System.out.println("x-- = " + (x--)); 17 | System.out.println("x = " + x); 18 | boolean flag = false; 19 | System.out.println("flag = " + flag); 20 | System.out.println("!flag = " + (!flag)); 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture 11/Question5.java: -------------------------------------------------------------------------------- 1 | Understanding the usage of Conditional Operators 2 | 3 | package q10833; 4 | public class ConditionalOperatorsDemo { 5 | public static void main(String[] args) { 6 | boolean noWorkInOffice = true; 7 | boolean isTodaySunday = false; 8 | boolean canTakeLeave = true; 9 | if (isTodaySunday || (noWorkInOffice && canTakeLeave)) { 10 | System.out.println("I am fishing..."); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Lecture 11/Question6.java: -------------------------------------------------------------------------------- 1 | There is yet another conditional operator called the ternary operator ?:. Its usage is 2 | condition ? expression1 : expression2 3 | The condition should always evaluate to a boolean. If the condition evaluates to true, expression1 is evaluated and its value returned, else expression2 is evaluated and its value returned. For example, 4 | int javaCertificationMarks = 75; 5 | int passMarks = 65; 6 | String result = (javaCertificationMarks > passMarks) ? "Passed Java Certification" : "Failed Java Certification"; 7 | 8 | In the above code, since the condition javaCertificationMarks > passMarks evaluates to true, result is assigned the value "Passed Java Certification" 9 | 10 | See and retype the below code to understand how ternary operator is used. 11 | 12 | Note: Please don't change the package name.' 13 | 14 | 15 | package q10834; 16 | public class TernaryOperatorsDemo { 17 | public static void main(String[] args) { 18 | int javaCertificationMarks = 75; 19 | int passMarks = 65; 20 | String result = (javaCertificationMarks > passMarks) ? "Passed Java Certification" : "Failed Java Certification"; 21 | System.out.println("result : " + result); 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 12/Question2.java: -------------------------------------------------------------------------------- 1 | In Java, there is a keyword instanceof, which is used to verify if an object is an instance of a particular class. 2 | Its usage is 3 | reference instanceof ClassName 4 | For example, the expression (args instanceof String[]) evaluates to true. 5 | 6 | The result of an expression using instanceof is always a boolean value (true or false). 7 | 8 | According to Java coding conventions, a single space should be provided to the left and right of this operator. 9 | 10 | Select all the valid statements in the below code: 11 | String name1 = "Ganga"; 12 | String name2 = "Amazon"; 13 | 14 | boolean statement1 = name1 instanceof String; 15 | 16 | boolean statement2 = name2 instanceof name1; 17 | 18 | boolean statement3 = name1 instanceof Object; 19 | 20 | boolean statement4 = args instanceof Object; 21 | 22 | boolean statement5 = args instanceof String[] 23 | 24 | 25 | 26 | Answer 27 | 28 | statement1 evaluates to true 29 | statement2 will not compile. 30 | statement5 evaluates to true -------------------------------------------------------------------------------- /Lecture 12/Question3.java: -------------------------------------------------------------------------------- 1 | Understanding the usage of instanceof Operator 2 | 3 | package q10836; 4 | public class InstanceOfOperatorDemo { 5 | public static void main(String[] args) { 6 | if (args instanceof String[]) { 7 | System.out.println("args instanceof String[] is true"); 8 | } 9 | if (args instanceof Object) { 10 | System.out.println("args instanceof Object is true"); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /Lecture 12/Question5.java: -------------------------------------------------------------------------------- 1 | The class PrinHello is created with a main method. The method receives river names as arguments. 2 | 3 | See and retype the below code to see the result. 4 | 5 | Note: Please don't change the package name. 6 | 7 | package q10839; 8 | public class PrintHello { 9 | public static void main(String[] args) { 10 | String suffix = "River"; 11 | for (String riverName : args) { 12 | System.out.println(riverName + suffix); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Lecture 12/Question6.java: -------------------------------------------------------------------------------- 1 | Java supports the below compound assignment operators 2 | += -= *= /= %= &= ^= |= <<= >>= >>>= 3 | 4 | Usage: 5 | 6 | int x = 3, y = 4, z = 0; 7 | 8 | z += x; // is same as writing z = z + x; 9 | z -= y; // is same as writing z = z - y;Select all the valid statements from the below code. 10 | int x = 2, y = 4, z = 5, value1 = 0, value2 = 0, value3 = 0; 11 | 12 | value1 *= x; 13 | 14 | value2 = x += 2 * y; 15 | 16 | y *= z; 17 | 18 | value3 -= 1; 19 | 20 | 21 | 22 | Answer 23 | 24 | value1 = 0 25 | 26 | y *= z; results in y's value being changed to 20 27 | 28 | value3 = -1 -------------------------------------------------------------------------------- /Lecture 14/Question3.java: -------------------------------------------------------------------------------- 1 | See the below code which uses a nested if-then-else statement for calculating grade for a given GPA. 2 | 3 | The class GradeCalculator has a method calculateGrade(double gpa). It receives a double argument gpa, a decimal number between 0.0 and 5.0. The method uses a if-then-else statement to display the appropriate Grade for the GPA. 4 | 5 | Add another condition that will print Grade: D when the given GPA is between 2.0 and 2.49 (both inclusive). 6 | 7 | Do use a the System.out.println() instead of System.out.print(). 8 | 9 | Note: Please don't change the package name. 10 | 11 | package q10848; 12 | public class GradeCalculator { 13 | public void calculateGrade(double gpa) { 14 | if (gpa >= 4.5) { 15 | System.out.println("Grade: A"); 16 | } else if (gpa >= 3.5 && gpa <= 4.49) { 17 | System.out.println("Grade: B"); 18 | } else if (gpa >= 2.5 && gpa <= 3.49) { 19 | System.out.println("Grade: C"); 20 | } else if (gpa>=2.0 && gpa<=2.49){ 21 | System.out.println("Grade: D"); 22 | }else { 23 | System.out.println("Grade: F"); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 14/Question4.java: -------------------------------------------------------------------------------- 1 | See the below code which uses a if-then-else statement for calculating AM or PM for a given hour. 2 | 3 | The class AmPmCalculator has a method checkAmPm it takes one parameter hour of type int, with value between 0 and 23. The method should use if-then-else statement to display AM or PM. 4 | 5 | Fill in the if condition to check if hour is between 0 and 11 (both inclusive) for AM. Fill in the else if condition to check if hour is between 12 and 23 (both inclusive) for PM 6 | 7 | Note: Use System.out.println() instead of System.out.print(). 8 | 9 | package q10849; 10 | public class AmPmCalculator { 11 | public void checkAmPm(int hour) { 12 | if (hour>=0 && hour<=11 ) { //fill the condition for AM here 13 | System.out.println("AM"); 14 | } else if (hour>=12 && hour<=23) { //fill the condition for PM here 15 | System.out.println("PM"); 16 | } else { 17 | System.out.println("Invalid Hour!!"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Lecture 14/Question5.java: -------------------------------------------------------------------------------- 1 | Write code which uses if-then-else statement to check if a given account balance is greater or lesser than the minimum balance. 2 | 3 | Write a class BalanceCheck with public method checkBalance that takes one parameter balance of type double. 4 | 5 | Use if-then-else statement and print Balance is low if balance is less than 1000. Otherwise, print Sufficient balance. 6 | 7 | Note: You need not write the main method. You can directly write the checkBalance(double balance) method in the BalanceCheck class. 8 | 9 | Use System.out.println() instead of System.out.print(). 10 | 11 | Note: Please don't change the package name. 12 | 13 | package q10850; 14 | 15 | class BalanceCheck{ 16 | public void checkBalance(double bal){ 17 | if(bal<1000){ 18 | System.out.println("Balance is low"); 19 | }else 20 | System.out.println("Sufficient balance"); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /Lecture 15/Question01.java: -------------------------------------------------------------------------------- 1 | Write a class IsWorkingDay with a main method. The method receives one command line argument which is the name of the weekday. 2 | 3 | Write the missing code in the If-else statement such that if the argument passed to the main method is equal to Sunday the program should print Holiday, otherwise it should print Working Day. 4 | 5 | For Example: 6 | Cmd Args : Sunday 7 | Holiday 8 | Note: We use public boolean equals(Object anObject) method to check if two strings have the same value. This method will be explained in more detail in later sections. 9 | 10 | Note: Please don't change the package name. 11 | 12 | 13 | package q10844; 14 | public class IsWorkingDay { 15 | public static void main(String[] args) { 16 | //Fill in the missing code using if-else statement 17 | if (args[0].equals("Sunday")){ 18 | System.out.println("Holiday"); 19 | }else{ 20 | System.out.println("Working Day"); 21 | } 22 | 23 | 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 15/Question02.java: -------------------------------------------------------------------------------- 1 | Create a class IsWorkingDay with a main method. The method receives one command line argument which is the name of the weekday. If the argument is equal to Sunday or Saturday the program should print Holiday, otherwise it should print Working Day. 2 | 3 | For Example: 4 | Cmd Args : Monday 5 | Working Day 6 | Note: Please don't change the package name. 7 | 8 | package q10845; 9 | class IsWorkingDay{ 10 | public static void main(String[] args){ 11 | if(args[0].equals("Sunday") || args[0].equals("Saturday")){ 12 | System.out.println("Holiday"); 13 | }else{ 14 | System.out.println("Working Day"); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Lecture 15/Question03.java: -------------------------------------------------------------------------------- 1 | Create a class SameNumbers with a public method checkNumbers that takes two parameters firstNo and secondNo are of type int and returns true if firstNo and secondNo are equal. The return type of checkNumbers should be boolean. 2 | 3 | For example: 4 | Cmd Args : 10 10 5 | true 6 | Cmd Args : 2 3 7 | false 8 | Note: Please don't change the package name. 9 | 10 | package q10864; 11 | 12 | public class SameNumbers { 13 | /** 14 | * Check if the two numbers are equal or not 15 | * 16 | * 17 | * 18 | * @return result 19 | */ 20 | 21 | 22 | public boolean checkNumbers(int a, int b) { 23 | //Write your code here 24 | if(a==b){ 25 | return true; 26 | }else{ 27 | return false; 28 | } 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Lecture 15/Question04.java: -------------------------------------------------------------------------------- 1 | Create a class PrintBiggerNumber with a public method checkNumbers that takes two parameters firstNo and secondNo are of type int and returns true if firstNo is greater than secondNo. The return type of checkNumbers should be boolean. 2 | 3 | These are examples for understanding: 4 | Cmd Args : 65 32 5 | true 6 | Cmd Args : 4 100 7 | false 8 | 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10865; 13 | 14 | public class PrintBiggerNumber { 15 | /** 16 | * Compare if firstNo is greater than secondNo are not 17 | * 18 | * 19 | * @return result 20 | * 21 | */ 22 | 23 | public boolean checkNumbers(int firstNo, int secondNo) { 24 | 25 | //Write your code here 26 | //write your code here 27 | 28 | if(firstNo >secondNo){ 29 | 30 | return true; 31 | } 32 | else{ 33 | 34 | return false; 35 | 36 | } 37 | 38 | 39 | 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /Lecture 15/Question05.java: -------------------------------------------------------------------------------- 1 | Create a class PrintSum with a public method computeSum that takes two parameters firstNo and secondNo are of type int. The program should print the sum of firstNo and secondNo. If firstNo and secondNo are equal it should double the sum and print the output. 2 | 3 | These are examples for understanding: 4 | Cmd Args : 23 5 5 | 28 6 | Cmd Args : 6 6 7 | 24 8 | 9 | 10 | 11 | 12 | Note: Please don't change the package name. 13 | 14 | package q10920; 15 | 16 | public class PrintSum{ 17 | 18 | public static void computeSum(int firstNo, int secondNo){ 19 | 20 | if(firstNo == secondNo){ 21 | 22 | System.out.println(firstNo+firstNo+secondNo+secondNo); 23 | } 24 | else{ 25 | 26 | System.out.println(firstNo+secondNo); 27 | 28 | } 29 | 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Lecture 15/Question06.java: -------------------------------------------------------------------------------- 1 | Create a class PrintDiff with a public method computeDiff that takes two parameters firstNo and secondNo are of type int. 2 | 3 | Calculate the difference between firstNo and secondNo. If the value of the difference is in between -25 and 25, the program should double the value and print the output, otherwise it should print the difference as it is. 4 | 5 | For example: 6 | Cmd Args : 15 7 7 | 16 8 | 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10921; 13 | 14 | public class PrintDiff { 15 | 16 | public void computeDiff(int firstNo, int secondNo) { 17 | 18 | int diff = firstNo -secondNo; 19 | 20 | if(diff>-25 && diff<25){ 21 | 22 | System.out.println(diff+diff); 23 | 24 | } 25 | 26 | else{ 27 | 28 | System.out.println(diff); 29 | 30 | } 31 | 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /Lecture 15/Question07.java: -------------------------------------------------------------------------------- 1 | Create a class PrintWorkingHour with a public method checkWorkingHour that takes one parameter hour of type int. 2 | 3 | If the hour is in between 9 and 17 (both inclusive) the program should print Working hour, otherwise it should print Non-working hour. 4 | 5 | For Example: 6 | Cmd Args : 14 7 | Working hour 8 | Note: Please don't change the package name. 9 | 10 | 11 | package q10922; 12 | 13 | public class PrintWorkingHour { 14 | 15 | public void checkWorkingHour(int hour) { 16 | 17 | //Nrite your code here 18 | 19 | if(hour>=9 && hour<=17){ 20 | 21 | System.out.println("Working hour"); 22 | 23 | }else{ 24 | 25 | System.out.println("Non-working hour"); 26 | 27 | } 28 | 29 | } 30 | 31 | 32 | 33 | } -------------------------------------------------------------------------------- /Lecture 15/Question08.java: -------------------------------------------------------------------------------- 1 | Create a class CheckSum100 with a public method checkSum that takes two parameters first and second are of type int. If one of the numbers is 100 or the sum of the numbers is 100 the program should print true, otherwise it should print false. 2 | 3 | For example: 4 | Cmd Args : 15 100 5 | true 6 | Note: Make sure to use println and not print method. 7 | 8 | Note: Please don't change the package name. 9 | 10 | package q10923; 11 | 12 | public class CheckSum100{ 13 | 14 | public static void checkSum( int first ,int second){ 15 | 16 | if(first==100 || second==100 || first+second==100){ 17 | 18 | System.out.println("true"); 19 | 20 | }else{ 21 | 22 | System.out.println("false"); 23 | 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Lecture 15/Question09.java: -------------------------------------------------------------------------------- 1 | Create a class CheckPositiveNegative with a public method checkPositiveNegative that takes two parameters first and second are of type int. 2 | 3 | Follow the given instructions while writing the code 4 | If both numbers are negative, the program should print negative 5 | If both numbers are positive, the program should print positive 6 | If one of the numbers is positive and the other is negative, the program should print mixed 7 | For example: 8 | Cmd Args : 1 2 9 | positive 10 | 11 | 12 | Note: Please don't change the package name. 13 | 14 | package q10924; 15 | 16 | public class CheckPositiveNegative{ 17 | 18 | public static void checkPositiveNegative(int first, int second){ 19 | 20 | if(first<0 && second<0) 21 | 22 | System.out.println("negative"); 23 | 24 | else if(first>0 && second>0) 25 | 26 | System.out.println("positive"); 27 | 28 | else 29 | 30 | System.out.println("mixed"); 31 | 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /Lecture 15/Question10.java: -------------------------------------------------------------------------------- 1 | Create a class CheckMultipleOf with a public method checkMultipleOf that takes two parameters first and second are of type int and returns true if first is multiple of second. The return type of checkMultipleOf is boolean. 2 | 3 | Here is an example: 4 | Cmd Args : 18 9 5 | true 6 | Hint: Use the % (modulus) operator. It can be used to find the remainder after division by a number. 7 | For example, to check if a given number is even we write 8 | if (number % 2 == 0) {// when divided by 2, if the reminder is zero, it is a even number 9 | System.out.println("number is even"); 10 | } 11 | 12 | 13 | Note: Please don't change the package name. 14 | ' 15 | package q10925; 16 | 17 | public class CheckMultipleOf{ 18 | 19 | public boolean checkMultipleOf(int f ,int s){ 20 | 21 | if(f%s==0){ 22 | 23 | return true; 24 | 25 | } 26 | 27 | else{ 28 | 29 | return false; 30 | 31 | } 32 | 33 | } 34 | 35 | 36 | 37 | } -------------------------------------------------------------------------------- /Lecture 15/Question11.java: -------------------------------------------------------------------------------- 1 | Create a class AgeCheck with a public method checkAge that takes two parameters age1 and age2 are of type int. The program should print Discount Price if the age1 is less than 10 and age2 is greater than 60, otherwise it should print Full Price. 2 | 3 | For Example: 4 | Cmd Args : 3 62 5 | Discount Price 6 | Note: Make sure to use println and not print method. 7 | 8 | Note: Please don't change the package name. 9 | 10 | package q10926; 11 | 12 | public class AgeCheck{ 13 | 14 | public static void checkAge(int al ,int a2){ 15 | 16 | if(al <10 && a2>60){ 17 | 18 | System.out.println("Discount Price"); 19 | 20 | }else{ 21 | 22 | System.out.println("Full Price"); 23 | 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Lecture 15/Question12.java: -------------------------------------------------------------------------------- 1 | Create a class RangeCheck with a public method checkRange that takes two parameters first and second are of type int. If one of the numbers is within the range of 25 to 35 (both inclusive) the program should print Within range, otherwise it should print Out of range. 2 | 3 | For example: 4 | Cmd Args : 25 79 5 | Within range 6 | Note: Please don't change the package name. 7 | 8 | package q10927; 9 | 10 | public class RangeCheck{ 11 | 12 | public static void checkRange(int f, int s){ 13 | 14 | if((f>=25 && f<=35) || (s>=25 && s<=35)){ 15 | 16 | System.out.println("Within range"); 17 | 18 | }else{ 19 | 20 | System.out.println("Out of range"); 21 | 22 | } 23 | 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 15/Question14.java: -------------------------------------------------------------------------------- 1 | Create a class CountSeniorCitizens with a public method countSeniorCitizens that takes four parameters age1, age2, age3, and age4 are of type int. The program should print the count of numbers which are greater than 60. 2 | 3 | For Example: 4 | Cmd Args : 60 61 59 58 5 | 1 6 | [Hint: You can use multiple if conditions to keep track of the count.] 7 | 8 | Note: Make sure to use println and not print method. 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10929; 13 | 14 | public class CountSeniorCitizens{ 15 | 16 | public static void countSeniorCitizens (int al, int a2, int a3, int a4){ 17 | 18 | int C=0; 19 | 20 | if(al>60){ 21 | 22 | C++; 23 | 24 | } 25 | 26 | if(a2>60){ 27 | 28 | C++; 29 | 30 | } 31 | 32 | if(a3>60){ 33 | 34 | C++; 35 | } 36 | if(a4>60){ 37 | 38 | C++; 39 | } 40 | System.out.println(C); 41 | 42 | 43 | 44 | 45 | } 46 | } -------------------------------------------------------------------------------- /Lecture 15/Question15.java: -------------------------------------------------------------------------------- 1 | Create a class FindMax with a public method findMax that takes three parameters arg1, arg2, and arg3 are of type int. The program should print the largest of the given numbers. 2 | 3 | For example: 4 | Cmd Args : 10 20 5 5 | 20 6 | Note: Please don't change the package name. 7 | 8 | package q10930; 9 | 10 | public class FindMax{ 11 | 12 | public void findMax(int a1, int a2, int a3){ 13 | 14 | if(a1>a2 && a1>a3){ 15 | 16 | System.out.println(a1); 17 | 18 | }else if(a2>a1 && a2>a3){ 19 | 20 | System.out.println(a2); 21 | 22 | }else{ 23 | 24 | System.out.println(a3); 25 | 26 | } 27 | 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Lecture 15/Question16.java: -------------------------------------------------------------------------------- 1 | Create a class FindNearestNumber with a public method findNearest that takes two parameters first and second are of type int. Find the number that is closer to 100, among first and second. 2 | 3 | For example: 4 | Cmd Args : 99 102 5 | 99 6 | 7 | 8 | Hint: There is a useful method called Math.abs(int number) which returns an absolute value for the number passed. 9 | For example: 10 | Math.abs(-3) will return 3 11 | Math.abs(4) will return 4 12 | You may want to use it. 13 | 14 | 15 | 16 | Note: Please don't change the package name. 17 | 18 | package q10931; 19 | 20 | public class FindNearestNumber{ 21 | 22 | public void findNearest(int f,int s){ 23 | 24 | if(Math.abs(100-f)>Math.abs (100-s)){ 25 | 26 | System.out.println(s); 27 | 28 | } 29 | 30 | else{ 31 | 32 | System.out.println(f); 33 | 34 | } 35 | 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Lecture 15/Question17.java: -------------------------------------------------------------------------------- 1 | Create a class NumberEndingCheck with a public method checkNumberEnding that takes two parameters first and second are of type int. The program should print true if both the first and second are ends with the same digit otherwise, it should print false. 2 | 3 | For Example: 4 | Cmd Args : 55 75 5 | true 6 | 7 | 8 | Hint: The % operator can be used to find the remainder after division by a number. 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10932; 13 | 14 | public class NumberEndingCheck{ 15 | 16 | public void checkNumberEnding(int f,int s){ 17 | 18 | if((f%10)==(s%10)){ 19 | 20 | System.out.println("true"); 21 | 22 | }else{ 23 | 24 | System.out.println("false"); 25 | 26 | } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture 15/Question18.java: -------------------------------------------------------------------------------- 1 | Given: 2 | String[] elements = { "abc", "xyz", "pqr" }; 3 | String first = (elements.length > 0) ? elements[0] : null; 4 | What is the result ? 5 | 6 | 7 | 8 | Answer 9 | 10 | The variable 'first' is set to "abc" -------------------------------------------------------------------------------- /Lecture 15/Question19.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String[] args) { 3 | Boolean test = new Boolean(true); 4 | Integer x = 343; 5 | Integer y = new Test().go(test, x); 6 | System.out.println(y); 7 | } 8 | 9 | int go(Boolean b, int i) { 10 | if (b) 11 | return (i / 7); 12 | return (i / 49); 13 | } 14 | } 15 | 16 | 17 | 18 | Answer 19 | 20 | 49 -------------------------------------------------------------------------------- /Lecture 16/Question6.java: -------------------------------------------------------------------------------- 1 | Write a class SumOfIntegers with a public method calcSum that takes one parameter arr of type int[] and returns the sum of all integers in the arr. The return type of calcSum should be int. 2 | 3 | Here is an example: 4 | Cmd Args : 1 3 5 7 9 5 | Sum = 25 6 | Fill in the missing in the below program. 7 | 8 | Note: Please don't change the package name. 9 | 10 | package q10876; 11 | public class SumOfIntegers { 12 | public int calcSum(int[] arr) { 13 | int sum = 0; 14 | // Fill in the missing code for the for-each statement given below 15 | for ( int value : arr) { 16 | sum += value; 17 | } 18 | return sum; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Lecture 16/Question7.java: -------------------------------------------------------------------------------- 1 | Write a class PrintEvenNumbers with a public method printEvenNumbers that takes one parameter arr of type int[]. 2 | 3 | Write code using for-each loop to iterate over the arr if the number is even the program should print is even otherwise, it should print is not even. 4 | 5 | Here is an example: 6 | Cmd Args : 13 14 15 7 | 13 is not even 8 | 14 is even 9 | 15 is not even 10 | Hint: You can use value % 2 == 0. Meaning, you can use the modulus operator (%) to divide a number by 2 and get the remainder, to verify if the reminder is equal to 0. 11 | 12 | 13 | 14 | Note: Please don't change the package name. 15 | 16 | package q10878; 17 | public class PrintEvenNumbers { 18 | 19 | public void printEvenNumbers(int[] arr) { 20 | for(int i : arr){ 21 | if(i%2 ==0){ 22 | System.out.println(i+" is even"); 23 | }else{ 24 | System.out.println(i+" is not even"); 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Lecture 16/Question8.java: -------------------------------------------------------------------------------- 1 | Write a class PrintOddNumbers with a public method printOddNumbers that takes one parameter arr of type int[]. 2 | 3 | Use for-each loop to iterate over the arr and the program should print is odd if the element is odd otherwise, it should print is not odd. 4 | 5 | For example: 6 | Cmd Args : 1 2 3 4 5 6 7 | 1 is odd 8 | 2 is not odd 9 | 3 is odd 10 | 4 is not odd 11 | 5 is odd 12 | 6 is not odd 13 | 14 | 15 | Note: Please don't change the package name. 16 | 17 | package q10879; 18 | public class PrintOddNumbers{ 19 | 20 | public static void printOddNumbers(int arr[]){ 21 | 22 | for (int i:arr){ 23 | 24 | if(i%2==0){ 25 | 26 | System.out.println(i+" is not odd"); 27 | 28 | }else{ 29 | 30 | System.out.println(i+" is odd"); 31 | } 32 | 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Lecture 17/Question02.java: -------------------------------------------------------------------------------- 1 | Write a class APowerN with a public method powerOf that takes two parameters A and N are of type int. Complete the missing code in the below program such that it should print the value of aPowerN. 2 | 3 | Assumptions: 4 | A and N are two positive and non-zero numbers 5 | For example: 6 | Cmd Args : 2 8 7 | APowerN = 256 8 | Hint: You can use a for loop to multiply A with itself N number of times. 9 | 10 | Variable aPowerN can be used to store the computed value of AN inside the for loop. 11 | 12 | package q10882; 13 | public class APowerN { 14 | public void powerOf(int A, int N) { 15 | int ans = 1; 16 | // Fill in the missing code using the for loop calculate A to the power N 17 | while( N!=0) { 18 | ans*=A; 19 | --N; 20 | } 21 | System.out.println("APowerN = " + ans); 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 17/Question03.java: -------------------------------------------------------------------------------- 1 | Factorial of a non-negative integer n is denoted by n!. It is the product of all positive integers less than or equal to n. 2 | 3 | Write a class FactorialCalculator with a public method factorial. The method receives one parameter n of type int. Fill in the missing code in the below program to calculate the factorial of a given number and print the output. 4 | 5 | For example: 6 | Cmd Args: 4 7 | Factorial of 4 = 24 8 | Hint: You can use the integer variable factorial initialized to 1, to store the computed factorial value. You can write a for loop which iterates from 2 to n multiplying the loop counter in each iteration with factorial and storing the product again in factorial. 9 | 10 | Note: Please don't change the package name. 11 | 12 | package q10883; 13 | public class FactorialCalculator { 14 | public void factorial(int n) { 15 | int factorial = 1; 16 | //Fill in the missing code using the for loop 17 | int i=1; 18 | while(i<=n){ 19 | factorial*=i; 20 | i++; 21 | } 22 | 23 | System.out.println("Factorial of " + n + " = " + factorial); 24 | } 25 | } -------------------------------------------------------------------------------- /Lecture 17/Question04.java: -------------------------------------------------------------------------------- 1 | Write a class PrintAlphabet with a main method. Write a logic to print all the English alphabets from A to Z. 2 | 3 | Hint: Use for loop to iterate over the characters from 'A' to 'Z'. 4 | 5 | You can declare and initialize a loop counter char i and initialize it to 'A' (eg: char i = 'A';). The condition can similarly be i <= 'Z';. 6 | And the update statement can be i++ then print the value of i. 7 | 8 | 9 | 10 | Note: Please don't change the package name' 11 | 12 | package q10884; 13 | class PrintAlphabet 14 | 15 | { 16 | 17 | public static void main(String args[]) 18 | 19 | { 20 | 21 | char ch; 22 | 23 | 24 | 25 | for( ch = 'A' ; ch <= 'Z' ; ch++ ) 26 | 27 | System.out.println(ch); 28 | 29 | } 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Lecture 17/Question05.java: -------------------------------------------------------------------------------- 1 | Write a class PrimeVerify with a public method checkPrimeOrNot that takes one parameter number of type int. Write a code to check whether the given number is a prime number or not. 2 | 3 | For example: 4 | Cmd Args : 13 5 | 13 is a prime number 6 | 7 | 8 | Note: Please don't change the package name. 9 | 10 | package q10885; 11 | 12 | public class PrimeVerify{ 13 | boolean isPrime(int n) 14 | 15 | { 16 | 17 | // Corner cases 18 | 19 | if (n <= 1) 20 | 21 | return false; 22 | 23 | if (n <= 3) 24 | 25 | return true; 26 | 27 | if (n % 2 == 0 || n % 3 == 0) 28 | 29 | return false; 30 | 31 | 32 | 33 | for (int i = 5; i * i <= n; i = i + 6) 34 | 35 | if (n % i == 0 || n % (i + 2) == 0) 36 | 37 | return false; 38 | 39 | 40 | 41 | return true; 42 | 43 | } 44 | 45 | 46 | public void checkPrimeOrNot(int n){ 47 | if(isPrime(n)){ 48 | System.out.println(n+" is a prime number"); 49 | }else{ 50 | System.out.println(n + " is not a prime number"); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Lecture 17/Question06.java: -------------------------------------------------------------------------------- 1 | Write a class Factorial with a main method. The method takes one command line argument. Write a logic to find the factorial of a given argument and print the output. 2 | 3 | For example: 4 | Cmd Args : 5 5 | Factorial of 5 is 120 6 | 7 | 8 | Note: Please don't change the package name. 9 | 10 | package q10886; 11 | class Factorial{ 12 | 13 | public static void main(String[] args ){ 14 | 15 | int i,fact=1; 16 | 17 | int number=Integer.parseInt(args[0]); 18 | 19 | for(i=1;i<=number;i++){ 20 | 21 | fact=fact*i; 22 | 23 | } 24 | 25 | System.out.println("Factorial of "+number+" is "+fact); 26 | 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Lecture 17/Question08.java: -------------------------------------------------------------------------------- 1 | Write a class PrintFiveTimes with a main method. The program should print Ganga five times. 2 | 3 | Fill the missing code in the below program so that it produces the desired output. 4 | 5 | Note: Please don't change the package name. 6 | 7 | package q10889; 8 | public class PrintFiveTimes { 9 | public static void main(String[] args) { 10 | int i = 0; 11 | while (i <5 ) { // complete the condition here 12 | System.out.println("Ganga");//write the text to be printed here 13 | i = i + 1; 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Lecture 17/Question09.java: -------------------------------------------------------------------------------- 1 | Write a class PrintThreeTimes with a main method. The program should print Thames three times. 2 | 3 | Fill the missing code so that it produces the desired output. 4 | 5 | Note: Please don't change the package name. 6 | 7 | package q10890; 8 | public class PrintThreeTimes { 9 | public static void main(String[] args) { 10 | int i = 0; 11 | while (i<3 ) { 12 | System.out.println("Thames"); 13 | i ++ ; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Lecture 17/Question11.java: -------------------------------------------------------------------------------- 1 | Write a class PrimeNumbers with a public method primeInLimits that takes two parameters high and low are of type int. Print the prime numbers between the given limits (including first and last values) 2 | 3 | For example: 4 | Cmd Args : 3 10 5 | 3 5 7 6 | Note: Please don't change the package name. 7 | 8 | package q10892; 9 | 10 | public class PrimeNumbers { 11 | 12 | public void primeInLimits(int start, int end) { 13 | for(int i = start ; i <= end ; i++) 14 | 15 | { 16 | 17 | //logic for checking number is prime or not 18 | 19 | int count = 0; 20 | 21 | for(int j = 1 ; j <= i ; j++) 22 | 23 | { 24 | 25 | if(i % j == 0) 26 | 27 | count = count+1; 28 | 29 | } 30 | 31 | if(count == 2) 32 | 33 | System.out.print(i+" "); 34 | 35 | } 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lecture 18/Question3.java: -------------------------------------------------------------------------------- 1 | The class MultiDimArrayPrinter with a main method. The program prints a multidimensional array of integers. 2 | 3 | The code in the main method uses a for-each loop to iterate over the multidimensional array intArr and prints the values. 4 | 5 | Understand and retype the code below. 6 | 7 | package q10946; 8 | public class MultiDimArrayPrinter { 9 | public static void main(String[] args) { 10 | int[][] int2DArr = { 11 | {1}, 12 | {2, 3}, 13 | {4, 5, 6}, 14 | {7, 8, 9, 10} 15 | }; 16 | for (int[] arr : int2DArr) { 17 | for (int value : arr) { 18 | System.out.print(value + " "); 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Lecture 18/Question4.java: -------------------------------------------------------------------------------- 1 | The class MultiDimArrayPrinter prints a multidimensional array of integers. 2 | 3 | The code in the main method uses for loops with loop counters to iterate over the multidimensional array int2DArr and print the values. 4 | 5 | Understand and retype the code below: 6 | 7 | package q10947; 8 | public class MultiDimArrayPrinter { 9 | public static void main(String[] args) { 10 | int[][] int2DArr = { 11 | {1}, 12 | {2, 3}, 13 | {4, 5, 6}, 14 | {7, 8, 9, 10} 15 | }; 16 | for (int i = 0; i < int2DArr.length; i++) { 17 | for (int j = 0; j < int2DArr[i].length; j++) { 18 | System.out.print(int2DArr[i][j] + " "); 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 18/Question6.java: -------------------------------------------------------------------------------- 1 | Write a class TransposeMatrix with a public method transposeMatrix that takes one parameter matrix1 of type int[][] which returns the transpose of the given matrix. 2 | 3 | Consider the following example for your undertanding: 4 | Matrix: 5 | Enter number of rows: 3 6 | Enter number of columns: 2 7 | Enter 2 numbers separated by space 8 | Enter row 1: 1 2 9 | Enter row 2: 3 4 10 | Enter row 3: 5 6 11 | Transpose of given matrices is: 12 | 1 3 5 13 | 2 4 6 14 | 15 | package q11105; 16 | public class TransposeMatrix { 17 | /** 18 | * Computes the transpose of the given matrix. 19 | * 20 | * @return the resultant matrix 21 | */ 22 | 23 | public int[][] transposeMatrix(int[][] m) { 24 | 25 | // Write the code 26 | // if(m.length<=0) return null; 27 | int copy[][]=new int [m[0].length][m.length]; 28 | for(int i=0;i= 0 && index < namesArr.length) { 12 | System.out.println(namesArr[index]); 13 | } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Lecture 19/Question4.java: -------------------------------------------------------------------------------- 1 | Create a class CityNameFinder with a public method findElement that takes one parameter index of type int. 2 | 3 | The findElement method should create an array namesArr containing these city names: Mumbai, Delhi, Kolkata, Chennai, Hyderabad, Bangalore. 4 | Write code in the method to print the element that is present at the index passed to the method. 5 | 6 | The program should print Wrong Index if the value of index is not in the range of valid indices of the array. 7 | 8 | For example: 9 | Cmd Args : 99 10 | Wrong Index 11 | 12 | package q10939; 13 | public class CityNameFinder { 14 | /** 15 | * Create an array which contains given names 16 | * 17 | * 18 | * @print the index element 19 | * 20 | */ 21 | 22 | public void findElement(int index) { 23 | //Write your code here 24 | 25 | 26 | String[] namesArr = { "Mumbai", "Delhi", "Kolkata", "Chennai", "Hyderabad", "Bangalore"}; 27 | 28 | if(index>=0 && index4){ 30 | flag= false; 31 | }else{ 32 | return true; 33 | } 34 | 35 | } 36 | 37 | return flag; 38 | 39 | 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /Lecture 21/Question09.java: -------------------------------------------------------------------------------- 1 | Write a class SumOfElements with a public method sum that takes one parameter arr of type int[] and returns sum of all positive elements in the arr. The return type of sum should be int. 2 | 3 | Assumptions: 4 | arr is never null 5 | arr may contain -ve numbers 6 | These are examples: 7 | Cmd Args : -35 -52 -12 -99 8 | Sum of all positive elements in the array is : 0 9 | Cmd Args : 36 12 -11 10 10 | Sum of all positive elements in the array is : 58 11 | 12 | package q11057; 13 | 14 | public class SumOfElements { 15 | /** 16 | * Compute sum of all +ve elements in the array excluding -ve numbers 17 | * 18 | * 19 | * @return sum 20 | */ 21 | 22 | public int sum(int[] arr) { 23 | 24 | //Write your code here 25 | int sum=0; 26 | for(int i=0;i=0){ 28 | 29 | 30 | sum+=arr[i]; 31 | } 32 | } 33 | return sum; 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Lecture 21/Question10.java: -------------------------------------------------------------------------------- 1 | Write a class SwapFirstAndLast with a public method swap that takes one parameter arr of type int[]. Write a code to swap the first and last elements of the array and print all the elements of the array. 2 | 3 | For example: 4 | Cmd Args : 1 5 6 7 8 5 | 8 6 | 5 7 | 6 8 | 7 9 | 1 10 | 11 | 12 | package q11058; 13 | public class SwapFirstAndLast{ 14 | public void swap(int[] a){ 15 | int f=a[0]; 16 | int l=a[a.length-1]; 17 | int temp=f; 18 | f=l; 19 | l=temp; 20 | System.out.println(f); 21 | for(int i=1;i1){ 33 | return true; 34 | } 35 | } 36 | } 37 | return false; 38 | } 39 | } -------------------------------------------------------------------------------- /Lecture 21/Question26.java: -------------------------------------------------------------------------------- 1 | Write a class SumOfSameNumber with a public method findSumOf that takes two parameters one is arr of type int[] and second one is arg of type int and returns true if sum of all arg elements present in the arr is greater than or equal to 10. 2 | 3 | Assumptions: 4 | arr is never null 5 | Here is an example: 6 | Enter no of elements in the array: 7 | 6 8 | Enter elements in the array seperated by space: 9 | 1 3 4 4 4 5 10 | Enter the search element: 11 | 4 12 | true 13 | 14 | package q11074; 15 | 16 | public class SumOfSameNumber { 17 | /** 18 | * Compute the sum of all arg elments in the arr is greater than 10 or not 19 | * 20 | * 21 | * 22 | * @return result 23 | */ 24 | 25 | public boolean findSumOf(int[] arr, int arg) { 26 | //Write your code here 27 | int count=0; 28 | for(int i=0;i=10) return true; 32 | else return false; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Lecture 21/Question28.java: -------------------------------------------------------------------------------- 1 | Write a class CheckAlternateNo with a public method checkAlternate that takes two parameters one is arr of type int[] and second one is arg of type int and returns true the element arg is present at every odd position of the arr. The return type of checkAlternate is boolean. 2 | 3 | Assumptions: 4 | arr is never null 5 | Here is an example: 6 | Enter no of elements in the array: 7 | 5 8 | Enter elements in the array seperated by space: 9 | 32 65 32 84 32 10 | Enter the arg element to find: 11 | 32 12 | true 13 | 14 | 15 | package q11076; 16 | 17 | public class CheckAlternateNo { 18 | /** 19 | * Check if the arg element is present at every odd postition of the arr 20 | * 21 | * 22 | * @return result 23 | */ 24 | 25 | public boolean checkAlternate(int[] arr, int arg) { 26 | 27 | //Write your code here 28 | int n=arr.length; 29 | int c1=0,c2=0; 30 | for(int i=0;i=0;i--){ 13 | ans+=s.charAt(i); 14 | } 15 | System.out.println(ans.trim()); 16 | } 17 | } -------------------------------------------------------------------------------- /Lecture 31/Question13.java: -------------------------------------------------------------------------------- 1 | Write a class MiddleTwoChars with a main method. The method receives one command line argument extract the middle two characters from the argument and print the output. 2 | 3 | Assumptions: 4 | The string has even number of characters 5 | For example: 6 | Cmd Args : java 7 | av 8 | Note: Make sure to use println and not print method. 9 | 10 | package q11203; 11 | public class MiddleTwoChars { 12 | public static void main(String[] args){ 13 | String str=args[0]; 14 | int position; 15 | 16 | int length; 17 | 18 | if (str.length() % 2 == 0) 19 | 20 | { 21 | 22 | position = str.length() / 2 - 1; 23 | 24 | length = 2; 25 | 26 | } 27 | 28 | else 29 | 30 | { 31 | 32 | position = str.length() / 2; 33 | 34 | length = 1; 35 | 36 | } 37 | 38 | System.out.println(str.substring(position, position + length)); 39 | 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /Lecture 31/Question14.java: -------------------------------------------------------------------------------- 1 | Write a class EndsWith with a main method. The method receives one command line argument. Print true if the argument ends with bad, else print false. 2 | 3 | For Example: 4 | Cmd Args : Hyderabad 5 | true 6 | Note: Make sure to use println and not print method. 7 | 8 | package q11204; 9 | public class EndsWith{ 10 | 11 | public static void main(String args[]){ 12 | 13 | String s1=args[0]; 14 | 15 | if(s1.endsWith("bad")) System.out.println("true") ; 16 | 17 | else System.out.println("false"); 18 | 19 | 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Lecture 31/Question15.java: -------------------------------------------------------------------------------- 1 | Write a class EndsWith with a main method. The method receives two command line arguments. Print true if the first argument ends with the second argument (ignoring case), else print false. 2 | 3 | Example: 4 | Cmd Args : Godavari ri 5 | true 6 | [Hint: Since we have to verify ignoring the case, you may want to first convert both either to lower or upper case before comparing.] 7 | 8 | 9 | package q11205; 10 | public class EndsWith { 11 | 12 | 13 | public static void main(String args[]){ 14 | 15 | String s1=args[0].toLowerCase(); 16 | 17 | if(s1.endsWith(args[1].toLowerCase())) System.out.println("true") ; 18 | 19 | else System.out.println("false"); 20 | 21 | 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Lecture 31/Question16.java: -------------------------------------------------------------------------------- 1 | Write a class TestString with a main method. The method receives one command line argument. Create a new string by concatenating the first three and last three characters of the argument and print the output. 2 | 3 | Assumptions: 4 | String length is at least six 5 | For Example: 6 | Cmd Args : Hyderabad 7 | Hydbad 8 | 9 | 10 | package q11206; 11 | 12 | public class TestString{ 13 | public static void main(String args[]){ 14 | 15 | System.out.println(args[0].substring(0,3)+args[0].substring(args[0].length()-3)); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Lecture 31/Question17.java: -------------------------------------------------------------------------------- 1 | Write a class TestString with a main method. The method receives one command line argument. Create a string by extracting the first character and appending it to the end of the argument , then append aa at the end. 2 | 3 | For example: 4 | Cmd Args : Europe 5 | uropeEaa 6 | Note: Make sure to use println and not print method. 7 | 8 | 9 | package q11207; 10 | public class TestString{ 11 | 12 | public static void main(String args[]){ 13 | 14 | 15 | System.out.println(args[0].substring(1)+args[0].charAt(0)+"aa"); 16 | } 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /Lecture 31/Question18.java: -------------------------------------------------------------------------------- 1 | Write a class MiddleChar with a main method. The method receives one command line argument. Print the middle character of the argument. 2 | 3 | Assumptions: 4 | The length of the string is odd 5 | Example: 6 | Cmd Args : Three 7 | r 8 | Note: Make sure to use println and not print method. 9 | 10 | 11 | package q11208; 12 | public class MiddleChar{ 13 | public static void main(String args[]){ 14 | System.out.println(args[0].charAt(args[0].length()/2)); 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /Lecture 31/Question19.java: -------------------------------------------------------------------------------- 1 | Write a class CheckChars with a main method. The method receives one command line argument and prints true if the argument has the characters w, e and b. 2 | 3 | For Example: 4 | Cmd Args : westbengal 5 | true 6 | 7 | 8 | package q11209; 9 | 10 | public class CheckChars{ 11 | 12 | public static void main(String args[]){ 13 | int cnt=0; 14 | for(int i=0;i3)?"true":"false"; 23 | 24 | System.out.println(ans); 25 | 26 | 27 | 28 | } 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /Lecture 31/Question20.java: -------------------------------------------------------------------------------- 1 | Write a class AppendChars with a main method. The method receives one commmand line argument. If the argument length is less than 10, append '*' at the end to make the length 10. 2 | 3 | For example: 4 | Cmd Args : Smile 5 | Smile***** 6 | 7 | 8 | package q11210; 9 | public class AppendChars { 10 | public static void main(String args[]){ 11 | String ans=args[0].substring(0); 12 | if(args[0].length()<10) { 13 | while(ans.length()<10) 14 | ans+="*"; 15 | } 16 | System.out.println(ans); 17 | 18 | 19 | 20 | } 21 | 22 | 23 | 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lecture 31/Question21.java: -------------------------------------------------------------------------------- 1 | Write a class RemoveChars with a main method. The method receives one command line argument and prints the output by removing all x characters from the argument. 2 | 3 | For example: 4 | Cmd Args : prefix 5 | prefi 6 | Note: Make sure to use println and not print method. 7 | 8 | 9 | package q11211; 10 | public class RemoveChars{ 11 | 12 | public static void main(String args[]){ 13 | 14 | String str = args[0].replace("x", ""); 15 | 16 | System.out.println(str); 17 | 18 | 19 | 20 | } 21 | 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Lecture 31/Question22.java: -------------------------------------------------------------------------------- 1 | Write a class RemoveSuffix with a main method. The method receives one command line argument. If the argument has the same prefix and suffixes up to 3 characters, remove the suffix and print the argument. 2 | 3 | Example: 4 | Cmd Args : systemsys 5 | system 6 | 7 | 8 | package q11212; 9 | public class RemoveSuffix{ 10 | 11 | public static void main(String[] args){ 12 | 13 | int n=args[0].length(); 14 | 15 | if(args[0].substring(0,3).equals(args[0].substring(n-3))) 16 | 17 | System.out.println(args[0].substring(0,n-3)); 18 | 19 | else System.out.println(args[0]); 20 | 21 | 22 | 23 | 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /Lecture 31/Question24.java: -------------------------------------------------------------------------------- 1 | Write a class CountChars with a main method. The method receives one command line argument and prints the number of o's (it is not zero, it is the alphabet 'o') present in the argument. 2 | 3 | Example: 4 | Cmd Args : ozone 5 | 2 6 | Note: Make sure to use println and not print method. 7 | 8 | 9 | package q11214; 10 | public class CountChars{ 11 | 12 | public static void main(String args[]){ 13 | 14 | String vivek =args[0] ; 15 | 16 | Character searchChar = 'o'; 17 | 18 | long count =vivek.chars().filter(ch -> ch == searchChar).count(); 19 | 20 | System.out.println(count); 21 | 22 | } 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lecture 31/Question25.java: -------------------------------------------------------------------------------- 1 | Write a class JavaStringBufferDelete with a main method to delete characters from a string using StringBuffer class. 2 | 3 | Follow the given instructions. 4 | Consider a string "Hello India" and delete 0 to 6 characters in that and print the result. 5 | Consider another string "Hello World", delete characters from position 0 to length of the entire string and print the result. 6 | Consider another string "Hello Java", remove 0th character and then print the result. 7 | 8 | 9 | 10 | package q11215; 11 | 12 | public class JavaStringBufferDelete{ 13 | 14 | public static void main(String args[]){ 15 | 16 | StringBuffer a=new StringBuffer("Hello India"); 17 | 18 | StringBuffer b=new StringBuffer("Hello World"); 19 | 20 | StringBuffer c=new StringBuffer("Hello Java"); 21 | 22 | 23 | 24 | System.out.println(a.delete(0,6)); 25 | 26 | System.out.println(b.delete(0,b.length())); 27 | 28 | System.out.println(c.delete(0,1)); 29 | 30 | } 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture 31/Question26.java: -------------------------------------------------------------------------------- 1 | Given : 2 | public static void main(String[] args) { 3 | String str = "null"; 4 | if (str == null) { 5 | System.out.println("null"); 6 | } else (str.length() == 0) { 7 | System.out.println("zero"); 8 | } else { 9 | System.out.println("some"); 10 | } 11 | } 12 | 13 | 14 | Answer 15 | 16 | Compilation fails. -------------------------------------------------------------------------------- /Lecture 32/Question2.java: -------------------------------------------------------------------------------- 1 | The Math class present in java.lang package contains many useful static methods. These static methods are also called as utility methods. 2 | 3 | Classes like Math that have only static methods are also called Utility classes. 4 | 5 | Note: Most of the methods in Math class use double as data type, so we will see fractional part in the results. 6 | 7 | See the retype the below code. 8 | 9 | 10 | package q11239; 11 | public class MathExamples { 12 | public static void main(String[] args) { 13 | double base = 4; 14 | double exponent = 3; 15 | System.out.println(base + " to the power " + exponent + " is : " + Math.pow(base, exponent)); 16 | System.out.println("Square root of " + base + " is : " + Math.sqrt(base)); 17 | } 18 | } -------------------------------------------------------------------------------- /Lecture 32/Question6.java: -------------------------------------------------------------------------------- 1 | Given : 2 | public static void main(String[] args) { 3 | Integer i = new Integer(1) + new Integer(2); 4 | switch(i) { 5 | case 3: 6 | System.out.println("three"); 7 | break; 8 | default: 9 | System.out.println("other"); 10 | break; 11 | } 12 | } 13 | What is the result? 14 | 15 | three 16 | 17 | -------------------------------------------------------------------------------- /Lecture 34/Question01.java: -------------------------------------------------------------------------------- 1 | Method overloading means the ability to have multiple methods with same name, which vary in their parameters. For example: 2 | public void concatenate(String text, int num) { 3 | return text + num; 4 | } 5 | public void concatenate(String text, boolean flag) { 6 | return text + flag; 7 | } 8 | public void concatenate(String text, char ch) { 9 | return text + ch; 10 | } 11 | In the above code, concatenate method is overloaded three times. 12 | 13 | Note: In the above example, the variation in the parameters list can be by their count or type or both. 14 | 15 | Select all the correct statements given below regarding methods present in the String class. 16 | 17 | [Hint: You can explore the methods present in the String class by clicking on String and scrolling down until you reach a section heading named Method Summary. In that you will see table containing a list of all methods in the String class.] 18 | 19 | 20 | 21 | Answer 22 | charAt method is not overloaded. 23 | indexOf method is overloaded 4 times. 24 | The String constructor is overloaded 15 times -------------------------------------------------------------------------------- /Lecture 34/Question04.java: -------------------------------------------------------------------------------- 1 | Write a Java program with a class name OverloadArea with overload methods area(float) and area(float, float) to find area of square and rectangle. 2 | 3 | Write the main method within the class and assume that it will receive a total of 2 command line arguments of type float. 4 | 5 | If the main() is provided with arguments : 1.34, 1.98 then the program should print the output as: 6 | Area of square for side in meters 1.34 : 1.7956 7 | Area of rectangle for length and breadth in meters 1.34, 1.98 : 2.6532001 8 | 9 | 10 | package q11268; 11 | public class OverloadArea { 12 | // Write the overload methods 13 | static float area(float a){ 14 | return a*a; 15 | } 16 | static float area(float a,float b){ 17 | return a*b; 18 | } 19 | public static void main (String[] args) { 20 | // Write the code 21 | Float f1 = Float.parseFloat(args[0]); 22 | Float f2 = Float.parseFloat(args[1]); 23 | System.out.println("Area of square for side in meters " + f1 + " : " +area(f1) ); // Fill in the missing code 24 | System.out.println("Area of rectangle for length and breadth in meters " + f1 + ", " + f2 + " : " +area(f1,f2) ); // Fill in the missing code 25 | } 26 | } -------------------------------------------------------------------------------- /Lecture 34/Question06.java: -------------------------------------------------------------------------------- 1 | Method overloading is a feature which allows multiple methods with same name and different parameters in the same class. 2 | 3 | Method overriding is a feature where we specialize (or modify) a method behaviour which is already present in the superclass. This takes places only when we have a class extending another class. 4 | 5 | Select all the correct statements from the below code: 6 | class A { 7 | public void printMe(int number) { // statement 1 8 | System.out.println(number); 9 | } 10 | public void printMe(boolean flag) { // statement 2 11 | System.out.println(flag); 12 | } 13 | public void printMe(int number, boolean flag) { // statement 3 14 | System.out.println(number + " : " + flag); 15 | } 16 | } 17 | class B extends A { 18 | public void printMe(int number) { // statement 4 19 | System.out.println("The double of " + number + " is : " + (number * 2)); 20 | } 21 | } 22 | 23 | Answer 24 | Statements 1, 2 and 3 in class A contain overloaded versions of method printMe. 25 | 26 | The method in statement 4, overrides the method declared in statement 1. 27 | -------------------------------------------------------------------------------- /Lecture 35/Question1.java: -------------------------------------------------------------------------------- 1 | The toString present in the Object class ensures that an object of any class in Java can be converted into a String representation. 2 | 3 | If we do not override the toString method, by default the toString method present in the Object's class will be called (which is of no much use). 4 | 5 | See and retype the below code and observe the output to understand the usage of toString method. 6 | 7 | You will also notice that when an object reference is concatenated with + operator to a string literal, the object reference is automatically converted to a string by invoking the toString method. 8 | 9 | That is how in the statement System.out.println("a : " + a);, "a : " + a is resulting in "a : " + a.toString(). 10 | 11 | 12 | 13 | package q11279; 14 | public class ToStringExample { 15 | public static void main(String[] args) { 16 | A a = new A(4); 17 | System.out.println("a.toString() : " + a.toString()); 18 | System.out.println("a : " + a); 19 | } 20 | } 21 | class A { 22 | private int value; 23 | public A(int value) { 24 | this.value = value; 25 | } 26 | public String toString() { 27 | return "The value is : " + value; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture 37/Question6.java: -------------------------------------------------------------------------------- 1 | values(): 2 | 3 | We can obtain an array of all the possible values of a Java enum type by calling its static method values(). All enum types get a static values() method automatically by the Java compiler. Here is an example of iterating all values of an enum: 4 | enum Subject{ 5 | JAVA, C, PYTHON 6 | } 7 | Iteration can be done for the above enum class using the method value() can be done by 8 | for(Subject sub : Subject.values()) { 9 | System.out.println(sub); 10 | } 11 | 12 | 13 | 14 | package q24199; 15 | public class Test { 16 | public static void main(String args[]) { 17 | 18 | // Iterate over the elements in enum and print them 19 | for(Languages v:Languages.values()) System.out.println(v); 20 | } 21 | } 22 | enum Languages { 23 | JAVA, 24 | PYTHON, 25 | C, 26 | CPP, 27 | DBMS 28 | } -------------------------------------------------------------------------------- /Lecture 37/Question7.java: -------------------------------------------------------------------------------- 1 | valueOf(): 2 | 3 | The valueOf() method can be used to obtain an instance of the enum class for a given String value. Here is an example. 4 | enum Subject{ 5 | JAVA, C, PYTHON 6 | } 7 | Iteration can be done for the above enum class using the method value() can be done by 8 | Subject sub = Subject.valueOf("JAVA"); 9 | } 10 | 11 | package q24200; 12 | public class Test { 13 | public static void main(String args[]) { 14 | 15 | Languages a = Languages.valueOf("JAVA"); 16 | 17 | Languages b = Languages.valueOf("PYTHON"); 18 | 19 | Languages c= Languages.valueOf("C"); 20 | 21 | Languages d = Languages.valueOf("CPP"); 22 | 23 | Languages eee = Languages.valueOf("DBMS"); 24 | System.out.println(a); 25 | 26 | System.out.println(b); 27 | 28 | System.out.println(c); 29 | 30 | System.out.println(d); 31 | 32 | System.out.println(eee); 33 | 34 | 35 | // print the instances of all enumeration constants in enum class Languages 36 | 37 | } 38 | } 39 | enum Languages { 40 | JAVA, 41 | PYTHON, 42 | C, 43 | CPP, 44 | DBMS 45 | } -------------------------------------------------------------------------------- /Lecture 37/Question9.java: -------------------------------------------------------------------------------- 1 | The below program has an enum class with Student which has marks of different students. Define a constructor and get the marks of each student in the following way. 2 | 3 | Total Marks: 4 | Radha got 90 marks. 5 | Seetha got 62 marks. 6 | Ram got 50 marks. 7 | 8 | 9 | 10 | package q24202; 11 | enum Student { 12 | Radha(90),Seetha(62),Ram(50); 13 | private int total; 14 | //write your code here 15 | //define a constructor Student and get total marks 16 | int marks; 17 | int getMarks(){ 18 | return marks; 19 | } 20 | Student (int marks){ 21 | this.marks=marks; 22 | } 23 | 24 | } 25 | public class Main { 26 | public static void main(String args[]){ 27 | System.out.println("Total Marks: "); 28 | 29 | // write your code here 30 | Student S; 31 | 32 | System.out.println("Radha got " +Student.Radha.getMarks()+ " marks."); 33 | 34 | System.out.println("Seetha got " +Student.Seetha.getMarks()+ " marks."); 35 | 36 | System.out.println("Ram got " +Student.Ram.getMarks()+ " marks."); 37 | 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Lecture 42/Question3.java: -------------------------------------------------------------------------------- 1 | In Java, when we do not want a method in a class to be overridden in its subclasses, we declare that method as final. 2 | 3 | We have many final methods in Object class. For example below code displays one of them: 4 | public class Object { 5 | ... 6 | ... 7 | public final void wait() throws InterruptedException { 8 | wait(0); 9 | } 10 | ... 11 | } 12 | Select all the correct statements from the below code: 13 | public class A { 14 | public void method1() { /*do something */ } // statement 1 15 | public final void method2() { /*do something */ } // statement 2 16 | public void method3() { /*do something */ } // statement 3 17 | } 18 | public class B extends A { 19 | public void method1() { /*do something else */ } // statement 4 20 | public void method2() { /*do something else */ } // statement 5 21 | public final void method3() { /*do something else */ } // statement 6 22 | } 23 | 24 | 25 | 26 | Answer 27 | 28 | method1 of class A in statement 1 is overridden in class B in statement 4. -------------------------------------------------------------------------------- /Lecture 43/Question1.java: -------------------------------------------------------------------------------- 1 | In Java, when we do not want a class to ever have subclasses, we declare that class as final. 2 | 3 | In Java we have many final classes. For example, like the String and all the wrapper classes like Integer, Float etc. 4 | 5 | 6 | Answer 7 | 8 | We declare a class as final, when we do not want someone to change its implementation by subclassing -------------------------------------------------------------------------------- /Lecture 43/Question2.java: -------------------------------------------------------------------------------- 1 | Select all the correct statements from the below code: 2 | public final class A { 3 | public void m1() { // statement 1 4 | ... 5 | } 6 | } 7 | public class B extends A { // statement 2 8 | } 9 | public class C { 10 | public void m2() { 11 | ... 12 | } 13 | } 14 | public class D extends C { 15 | public final void m2() { // statement 3 16 | final int value = 2; 17 | final int[] valueArr = {2, 3, 4}; 18 | final String text1 = "Cat on the wall"; 19 | String text2 = text1; // statement 4 20 | String text3 = "Dog in the pool"; 21 | text1 = text3; // statement 5 22 | valueArr[1] = value; // statement 6 23 | } 24 | } 25 | 26 | 27 | 28 | Answer 29 | 30 | In statement 2, will result in compilation error. Since class A is declared as final it cannot be extended by class B. 31 | 32 | Statement 5 will result in a compilation error. -------------------------------------------------------------------------------- /Lecture 44/Question02.java: -------------------------------------------------------------------------------- 1 | In Java, the constructors are used to prepare a newly created object for use by initilaizing values passed to it as parameters. 2 | 3 | Similarly, when the GC (Garbage Collector) decides to remove an object from memory, it calls the finalize() method on the object. 4 | 5 | The finalize() method is declared in the Object class. Hence it is available in every class. 6 | 7 | The default finalize() method available in the Object class does not do anything. 8 | 9 | A Java class can override and provide its special implementation in the finalize() method. 10 | 11 | Normally we do not override finalize to provide any special implementation. However, it is good to know that we do not call the finalize() method, it is the GC which calls it. 12 | 13 | 14 | Answer 15 | 16 | GC (Garbage Collector Thread) automatically calls the finalize method of the object whenever it is trying to reclaim the memory occupied by the object. -------------------------------------------------------------------------------- /Lecture 44/Question06.java: -------------------------------------------------------------------------------- 1 | Given 2 | public class Main { 3 | public static void main(String args[]) { 4 | StringBuilder sb = new StringBuilder("Hi! Good Morning."); 5 | System.out.println(sb.length()); 6 | } 7 | } 8 | Choose the correct output for the above program form the below options. 9 | 10 | Answer 11 | 12 | 17 -------------------------------------------------------------------------------- /Lecture 44/Question07.java: -------------------------------------------------------------------------------- 1 | Given 2 | public class Main { 3 | public static void main(String args[]) { 4 | StringBuilder sb = new StringBuilder("Hi! Good Morning."); 5 | 6 | } 7 | } 8 | Write an expression that refers to the letter M in the string referred to by sb. Choose the correct option from the below. 9 | 10 | 11 | Answer 12 | 13 | sb.charAt(9) -------------------------------------------------------------------------------- /Lecture 44/Question08.java: -------------------------------------------------------------------------------- 1 | The program has a class Example with the main method. The program takes input from the command line argument. Print the output by appending all the capital letters in the input. 2 | 3 | Sample Input and Output: 4 | Cmd Args : HYderaBad 5 | The result is: HYB 6 | 7 | 8 | package q24212; 9 | 10 | public class Example{ 11 | public static void main(String args[]){ 12 | String inputString=args[0]; 13 | 14 | 15 | 16 | String outputString = ""; 17 | 18 | 19 | 20 | for (int i = 0; i < inputString.length(); i++) { 21 | 22 | char c = inputString.charAt(i); 23 | 24 | outputString += Character.isUpperCase(c) ? c + "" : ""; 25 | 26 | } 27 | 28 | System.out.println("The result is: "+ outputString); 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Lecture 44/Question10.java: -------------------------------------------------------------------------------- 1 | Given 2 | public class StringBuilderDemo { 3 | public static void main(String args[]) { 4 | String s1 = new String("ABC"); 5 | String s2 = new String("ABC"); 6 | System.out.println(s1.equals(s2)); 7 | StringBuilder sb1 = new StringBuilder("ABC"); 8 | StringBuilder sb2 = new StringBuilder("ABC"); 9 | System.out.println(sb1.equals(sb2)); 10 | } 11 | } 12 | What will be the output for the above program. Choose the correct option form the below. 13 | 14 | 15 | 16 | Answer 17 | 18 | true 19 | false -------------------------------------------------------------------------------- /Lecture 44/Question11.java: -------------------------------------------------------------------------------- 1 | public class StringBuilderDemo { 2 | public static void main(String args[]) { 3 | String s = "Hello"; 4 | s.concat("World"); 5 | System.out.println(s); 6 | } 7 | } 8 | What will be the output for the above program. Choose the correct option form the below. 9 | 10 | 11 | 12 | Answer 13 | 14 | 15 | Hello -------------------------------------------------------------------------------- /Lecture 44/Question12.java: -------------------------------------------------------------------------------- 1 | Given 2 | public class StringBuilderDemo { 3 | public static void main(String args[]) { 4 | StringBuilder sb = new StringBuilder("Hello "); 5 | sb.append("World"); 6 | System.out.println(sb); 7 | } 8 | } 9 | What will be the output for the above program. Choose the correct option form the below. 10 | 11 | 12 | Answer 13 | 14 | Hello World -------------------------------------------------------------------------------- /Lecture 45/Question1.java: -------------------------------------------------------------------------------- 1 | Among the various classes available in java.lang package, System is one of the most commonly used classes after String. 2 | 3 | The System class can neither be instantiated (since it has a private constructor) nor can be extended (since it is declared as final). 4 | 5 | System class has three important public static fields: 6 | out - is accessed as System.out. This out field is of type PrintStream. The out refers to the standard output stream. 7 | err - is accessed as System.err. This err field is of type PrintStream. The err refers to the standard error stream. 8 | in - is accessed as System.in. This in field is of type InputStream. The in refers to the standard input stream. 9 | The System class provides a method called System.console(), which returns the java.io.Console object associated with the running Java process (Java Virtual Machine). Select all the correct statement given below. 10 | 11 | 12 | 13 | Answer 14 | 15 | In the statement 16 | System.out.println("Uranus"); 17 | println is a method. -------------------------------------------------------------------------------- /Lecture 45/Question2.java: -------------------------------------------------------------------------------- 1 | Select all the correct statements for some of the common methods in System class. [Hint: Make sure to click on the method names and read the method documentation before you mark the answers]. 2 | 3 | 4 | 5 | Answer 6 | 7 | ALL 8 | 9 | currentTimeMillis() is a static method in System class. So we can directly call the method on the class name, as given below: 10 | System.currentTimeMillis(); 11 | The currentTimeMillis() method in System class returns a long value representing the total time elapsed from the midnight of January 1, 1970 UTC until the current system time. 12 | The currentTimeMillis() method in System class returns a long value with precision upto nanoseconds. We can use this method when we want to measure time difference between two events to the precision of nanoseconds. 13 | 14 | int[] sourceArr = {1, 3, 5, 7, 9}; 15 | int[] destinationArr = new int[5]; 16 | System.arraycopy(sourceArr, 1, destinationArr, 1, 2); 17 | After the arraycopy method invocation, the values in destinationArr array will be {0, 3, 5, 0, 0} -------------------------------------------------------------------------------- /Lecture 45/Question3.java: -------------------------------------------------------------------------------- 1 | Select all the correct statements for the below code: 2 | public class Demo { 3 | public static void main(String[] args) { 4 | System.out.print("Up "); 5 | System.out.print("up"); 6 | System.out.print(" and away!"); 7 | } 8 | } 9 | 10 | 11 | 12 | Answers 13 | 14 | The output will be as given below: 15 | Up up and away! -------------------------------------------------------------------------------- /Lecture 45/Question4.java: -------------------------------------------------------------------------------- 1 | In many situations we would like to measure the time taken to perform a certain operation. Below is an example which uses the methods present in the System class to measure the time taken. 2 | 3 | See and retype the below code to understand the usage of System.currentTimeMillis() and System.nanoTime() 4 | 5 | 6 | 7 | package q11312; 8 | public class TimingExample { 9 | public static void main(String[] args) { 10 | long startTime = System.currentTimeMillis(); 11 | int total = 0; 12 | for (int i = 0; i < 10000; i++) { 13 | total = total + i; 14 | } 15 | long endTime = System.currentTimeMillis(); 16 | System.out.println("Time taken in milliseconds = " + (endTime - startTime)); 17 | } 18 | } -------------------------------------------------------------------------------- /Lecture 46/Question09.java: -------------------------------------------------------------------------------- 1 | Given : 2 | import java.util.Date; 3 | import java.text.DateFormat; 4 | 5 | DateFormat df; 6 | Date date = new Date(); 7 | // insert code here 8 | String s = df.format(date); 9 | Which code fragment, inserted at // insert code here, allows the code to compile? 10 | 11 | 12 | Answer 13 | 14 | 15 | df = DateFormat.getInstance(); 16 | -------------------------------------------------------------------------------- /Lecture 46/Question11.java: -------------------------------------------------------------------------------- 1 | The Random class in java.util package can be used to generate pseudo random numbers. 2 | 3 | See and retype the below code which generates 5 random numbers below 100. Click on the class Random to know more about the available methods. 4 | 5 | 6 | 7 | package q11316; 8 | import java.util.*; 9 | public class RandomDemo { 10 | public static void main(String[] args) { 11 | Random rand = new Random(10); 12 | for (int i = 0; i < 5; i++) { 13 | System.out.println(rand.nextInt(100)); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Lecture 47/Question4.java: -------------------------------------------------------------------------------- 1 | Click Submit to find out the problem. 2 | 3 | The code should actually print the fourth element passed in the arguments to the main method. 4 | 5 | [Hint: Click on the animating blue arrow in the exception stack trace to understand the error.] 6 | 7 | 8 | 9 | package q11320; 10 | public class ExceptionDemo4 { 11 | public static void main(String args[]) { 12 | System.out.println(args[3]); 13 | } 14 | } -------------------------------------------------------------------------------- /Lecture 48/Question2.java: -------------------------------------------------------------------------------- 1 | The try block can have zero or more catch blocks. However, a try block can have zero or only one finally block. 2 | 3 | Note that if a try block has one or more catch blocks, then the finally block should be written only after the last catch block. 4 | 5 | The finally block cannot appear before or in between the catch blocks. 6 | 7 | Valid 8 | try {// try block} catch (ExceptionName1referenceName1) {// catch block} catch (ExceptionName2referenceName2) {// another catch block} finally {// finally block} 9 | Valid 10 | try {// try block} catch (ExceptionClassNamereferenceName) {// catch block} 11 | Valid 12 | try {// try block} finally {// finally block}Select all the correct usages of try, catch and finally blocks given below: 13 | 14 | 15 | 16 | 17 | Answer 18 | 19 | try { 20 | ... 21 | } catch (ExceptionClassName referenceName) { 22 | ... 23 | } finally { 24 | ... 25 | } 26 | 27 | 28 | 29 | 30 | try { 31 | ... 32 | } finally { 33 | ... 34 | } -------------------------------------------------------------------------------- /Lecture 51/Question2.java: -------------------------------------------------------------------------------- 1 | Given: 2 | class SequenceGenerator { 3 | synchronized void sequence(long n) { 4 | for (int i = 1; i < 3; i++) 5 | System.out.print(n + "-" + i + " "); 6 | } 7 | } 8 | 9 | public class Tester implements Runnable { 10 | static SequenceGenerator sg = new SequenceGenerator (); 11 | 12 | public static void main(String[] args) { 13 | new Thread(new Tester()).start(); 14 | new Thread(new Tester()).start(); 15 | } 16 | 17 | public void run() { 18 | sg.sequence(Thread.currentThread().getId()); 19 | } 20 | } 21 | Which of the following statements are true? 22 | 23 | 24 | Answer 25 | 26 | The output could be 6-1 6-2 5-1 5-2 27 | -------------------------------------------------------------------------------- /Lecture 51/Question4.java: -------------------------------------------------------------------------------- 1 | Read the code below: 2 | void waitForSignal() throws InterruptedException { 3 | Object obj = new Object(); 4 | synchronized (Thread.currentThread()) { 5 | obj.wait(); 6 | obj.notify(); 7 | } 8 | } 9 | Which statement is true? 10 | 11 | 12 | Answer 13 | 14 | The code throws IllegalMonitorStateException. -------------------------------------------------------------------------------- /Lecture 52/Question2.java: -------------------------------------------------------------------------------- 1 | Read the following code and select all the correct outputs: 2 | public class MyRunnable implements Runnable { 3 | public void run() { 4 | System.out.println("In run() method "); 5 | throw new RuntimeException("some problem"); 6 | } 7 | 8 | public static void main(String[] args) { 9 | Thread t1 = new Thread(new MyRunnable()); 10 | t1.start(); 11 | System.out.println("End of main() method"); 12 | } 13 | } 14 | 15 | 16 | Answer 17 | 18 | In run() method 19 | End of main() method 20 | java.lang.RuntimeException: some problem 21 | 22 | 23 | In run() method 24 | java.lang.RuntimeException: some problem 25 | End of main() method 26 | 27 | 28 | End of main() method 29 | In run() method 30 | java.lang.RuntimeException: some problem -------------------------------------------------------------------------------- /Lecture 55/Question5.java: -------------------------------------------------------------------------------- 1 | public interface House { 2 | @Deprecated 3 | void open(); 4 | void openFrontDoor(); 5 | void openBackDoor() 6 | } 7 | public class MyHouse implements House { 8 | public void open() {} 9 | public void openFrontDoor() {} 10 | public void openBackDoor() {} 11 | } 12 | If you compile this program, the compiler produces a warning because open was deprecated in the interface. What can you do to get rid of that warning?. Choose the correct answer from the following. 13 | 14 | 15 | 16 | Answers 17 | 18 | Suppress the warning by using @SuppressWarnings("deprecation") -------------------------------------------------------------------------------- /Lecture 56/Question2.java: -------------------------------------------------------------------------------- 1 | In Java, Collection is the root interface for the collection hierarchy. 2 | 3 | The main interfaces in the collection framework are given below: 4 | Collection 5 | | 6 | |____________Set 7 | | |________SortedSet 8 | |_____________List 9 | | 10 | |_____________Queue 11 | | |________Deque 12 | |_____________Map 13 | |________SortedMapYou will notice from the above hierarchy that even though Map is not a subtype of Collection interface, Map is part of the collection framework. 14 | 15 | The Java collection framework consists of collection hierarchy, Map interface with its hierarchy along with many other utility classes present in the java.util package like Date, StringTokenizer etc. 16 | 17 | The three main sub-interfaces of Collection interface are Set, List and Queue. We will learn in detail about each interface in the ensuing sections. 18 | 19 | Select all the correct statements. 20 | 21 | 22 | 23 | Answer 24 | 25 | List is a subtype of Collection. -------------------------------------------------------------------------------- /Lecture 56/Question4.java: -------------------------------------------------------------------------------- 1 | The interfaces Set, List and Queue extend Collection. Some of these also add additional methods apart from the methods present in Collection interface. 2 | 3 | A Set does not allow duplicate elements. Meaning, when the add method is called by passing an element which is already present, the underlying Set implementation does not add the element again and returns false (false indicates that the collection was not modified by the add method call). 4 | 5 | A List is essentially a flexible array. It maintains the order of elements and also allows index-based access of elements like an array (which a Set does not). It allows duplicate elements. 6 | 7 | A Queue interface provides methods for a queue data structure implementing first-in-first-out (FIFO). There are special implementations of Queue interface like PriorityQueue, which may alter this behaviour. 8 | 9 | A Map is a collection of key-value pairs and not just elements. 10 | 11 | 12 | Answers 13 | 14 | A Set does not accept duplicate elements. 15 | A List allows index-based access of elements. -------------------------------------------------------------------------------- /Lecture 56/Question6.java: -------------------------------------------------------------------------------- 1 | The Iterable interface has a method called iterator(), which returns an object of type Iterator. 2 | 3 | The Iterator interface has two methods hasNext() and next() which can also be used for iterating over all the elements present in a collection. See and retype the below code which has an example for iterating over ArrayList using an Iterator. 4 | 5 | Note: Whenever we use collection classes, we need to import the relevant classes or import all classes in the util package using a statement like: import java.util.*; 6 | 7 | 8 | 9 | package q11365; 10 | import java.util.*; 11 | public class IteratorDemo { 12 | public static void main(String[] args) { 13 | List planetsList = new ArrayList(); 14 | planetsList.add("Mercury"); 15 | planetsList.add("Venus"); 16 | planetsList.add("Earth"); 17 | planetsList.add("Mars"); 18 | planetsList.add("Jupiter"); 19 | planetsList.add("Saturn"); 20 | planetsList.add("Uranus"); 21 | planetsList.add("Neptune"); 22 | planetsList.add("Pluto"); 23 | Iterator itr = planetsList.iterator(); 24 | while (itr.hasNext()) { 25 | System.out.println(itr.next()); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Lecture 57/Question04.java: -------------------------------------------------------------------------------- 1 | See and retype the below code to familiarize yourself with some of the commonly used methods in ArrayList. 2 | 3 | The class iterates through all the arguments passed to the main method, and stores them into an ArrayList, which is later manipulated using its methods. 4 | 5 | Correlate the code and output to understand the usage of the methods. 6 | 7 | 8 | 9 | package q11369; 10 | import java.util.*; 11 | public class ArrayListMethodsDemo { 12 | public static void main(String[] args) { 13 | List aList = new ArrayList(args.length); 14 | for (String argument : args) { 15 | aList.add(argument); 16 | } 17 | System.out.println("aList = " + aList); 18 | System.out.println("aList.size() = " + aList.size()); 19 | Object removedElement = aList.remove(3); 20 | System.out.println("removedElement = " + removedElement); 21 | System.out.println("aList = " + aList); 22 | Object replacedElement = aList.set(0, "Steve Jobs"); 23 | System.out.println("aList = " + aList); 24 | aList.add(0, "Bill Gates"); 25 | System.out.println("aList = " + aList); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Lecture 57/Question06.java: -------------------------------------------------------------------------------- 1 | Write a program to understand how to remove elements from an ArrayList using the remove method . 2 | 3 | Create a class ListDemo with a main method. Follow the given instructions while writing the program: 4 | Create an ArrayList with the following elements: Mercury, Venus, Earth, Mars. 5 | Print all the elements in the list 6 | Remove the element at index 1 7 | Print the resultant list 8 | The result should be as follows: 9 | [Mercury, Venus, Earth, Mars] 10 | [Mercury, Earth, Mars] 11 | 12 | 13 | 14 | package q11957; 15 | import java.util.*; 16 | public class ListDemo { 17 | public static void main(String[] args) { 18 | List namesList = new ArrayList(); 19 | // Write your code here 20 | 21 | 22 | 23 | namesList.add("Mercury"); 24 | 25 | namesList.add("Venus"); 26 | 27 | namesList.add("Earth"); 28 | 29 | namesList.add("Mars"); 30 | 31 | System.out.println(namesList); 32 | 33 | namesList.remove(1); 34 | 35 | 36 | 37 | 38 | 39 | System.out.println(namesList); 40 | } 41 | } 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Lecture 57/Question07.java: -------------------------------------------------------------------------------- 1 | Write a program to understand how to retrive an element in an ArrayList using the method get. 2 | 3 | Create a class ListDemo with a main method and get the element at index 1. 4 | 5 | 6 | 7 | package q11958; 8 | import java.util.*; 9 | public class ListDemo { 10 | public static void main(String[] args) { 11 | List namesList = new ArrayList(); 12 | namesList.add("Mercury"); 13 | namesList.add("Venus"); 14 | namesList.add("Earth"); 15 | namesList.add("Mars"); 16 | System.out.println(namesList.get(1)); // write your logic here 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lecture 57/Question08.java: -------------------------------------------------------------------------------- 1 | Create a class ListDemo with a main method. Create an instance of ArrayList and change the element at the given index position using the set method. 2 | 3 | Write the missing code in the below program. Follow the instructions given in the program. 4 | 5 | 6 | 7 | package q11959; 8 | import java.util.*; 9 | public class ListDemo { 10 | public static void main(String[] args) { 11 | List namesList = new ArrayList(); 12 | namesList.add("Mercury"); 13 | namesList.add("Venus"); 14 | namesList.add("Earth"); 15 | namesList.add("Mars"); 16 | System.out.println(namesList); 17 | 18 | // change the element at index 0 to Sun 19 | namesList.set(0,"Sun"); 20 | 21 | // change the element at index 2 to Jupiter 22 | 23 | namesList.set(2,"Jupiter"); 24 | System.out.println(namesList); 25 | 26 | } 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture 57/Question11.java: -------------------------------------------------------------------------------- 1 | Create a class ListDemo with a main method. The method takes inputs from the command line arguments. Create an instance of ArrayList and add these inputs to the the list and print the same. 2 | 3 | Sample Input and Output: 4 | Cmd Args : Ganga Yamuna Krishna Godavari 5 | [Ganga, Yamuna, Krishna, Godavari] 6 | 7 | 8 | 9 | package q24079; 10 | import java.util.*; 11 | public class ListDemo { 12 | public static void main(String[] args) { 13 | // Notice the use of generics. We will learn more about them later. 14 | // The type parameter will ensure that your code cannot add any 15 | // other object than those of type String 16 | List namesList = new ArrayList(); 17 | // write your code below this 18 | 19 | for(int i=0;i<=3;i++) namesList.add(args[i]); 20 | 21 | // write your code above this 22 | System.out.println(namesList); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Lecture 57/Question14.java: -------------------------------------------------------------------------------- 1 | Create a class ListDemo with a main method. The method takes inputs from the command line arguments. Create an instance of ArrayList and add these inputs to the the list. Iterate over the ArrayList using an Iterator, and print the output as shown in the example. 2 | 3 | Sample Input and Output: 4 | Cmd Args : Rama Krishna Seetha Radha Lakshmi 5 | Rama 6 | Krishna 7 | Seetha 8 | Radha 9 | Lakshmi 10 | 11 | 12 | package q24083; 13 | import java.util.*; 14 | public class ListDemo { 15 | public static void main(String[] args) { 16 | // Notice the use of generics. We will learn more about them later. 17 | // The type parameter will ensure that your code cannot add any 18 | // other object than those of type String 19 | List namesList = new ArrayList(); 20 | // write your code below this 21 | 22 | for(int i=0;i itr2=namesList.iterator(); 24 | Iterator itr =namesList.iterator();// Write your code here 25 | while (itr2.hasNext() ) { 26 | System.out.println(itr2.next()); // print the list 27 | } 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /Lecture 57/Question16.java: -------------------------------------------------------------------------------- 1 | Fill the missing code in the below program to learn how to iterate over the elements stored in a ArrayList. 2 | 3 | Write a Java program with a class name ArrayListIterationDemo with a main method. The method takes inputs from the command line arguments. If the first character of the argument is in upperrcase add it to the namesList and print all the elements in the list. 4 | 5 | 6 | 7 | package q11955; 8 | import java.util.*; 9 | public class ArrayListIterationDemo { 10 | public static void main(String[] args) { 11 | List namesList = new ArrayList(); 12 | for (int k=0;k ans = new ArrayList < String > (); 19 | 20 | for(int k = 0; k < args.length;k++) ans.add (args[k]); 21 | 22 | for(int i = 0; i nameList = new ArrayList < String > (); 16 | 17 | for (int i = 0; i namesList = new ArrayList(); 15 | // write your code here 16 | 17 | List < String > ans = new ArrayList < String > (); 18 | 19 | for (int i = 0; i namesMap = new HashMap(); 17 | namesMap.put("Sun", "Sunday"); 18 | namesMap.put("Mon", "Monday"); 19 | namesMap.put("Tue", "Tuesday"); 20 | namesMap.put("Thu", "Thursday"); 21 | System.out.println("namesMap = " + namesMap); 22 | 23 | // write your code here 24 | 25 | System.out.println("value mapped to Tue is : " + namesMap.get("Tue")); 26 | 27 | 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Lecture 58/Question12.java: -------------------------------------------------------------------------------- 1 | Create a class HashMapIterationDemo with a main method. The method takes inputs from the command line arguments. From the input make the first two chars of the argument as key and the entire argument as value. Print the result as shown in the example. 2 | 3 | We can assume the size of names passed as arguments will be greater than three characters. 4 | 5 | Sample Input and Output 6 | Cmd Args : Red White Black Brown 7 | {Br=Brown, Wh=White, Re=Red, Bl=Black} 8 | 9 | 10 | package q24086; 11 | import java.util.*; 12 | public class HashMapIterationDemo { 13 | public static void main(String[] args) { 14 | Map namesMap = new HashMap (); 15 | Set shortNamesSet = namesMap.keySet(); 16 | for(int i=0;ikeys = namesMap.keySet(); 38 | 39 | 40 | System.out.println(namesMap); 41 | 42 | 43 | 44 | 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /Lecture 58/Question15.java: -------------------------------------------------------------------------------- 1 | Create a class CharcountDemo with a main method. The program had given input string CodeTantra. Write a program to count the occurrence of each character in the given string using Hashmap. Fill the missing code in the below program. 2 | 3 | Sample Test Cases 4 | Test Case 1: 5 | Expected Output: 6 | {a=2,·r=1,·C=1,·d=1,·T=1,·t=1,·e=1,·n=1,·o=1} 7 | 8 | 9 | package q24099; 10 | import java.util.*; 11 | public class CharcountDemo { 12 | public static void main(String[] args) { 13 | String str = "CodeTantra"; 14 | HashMap namesMap = new HashMap(); 15 | 16 | // write your code here 17 | char[] str_array = str.toCharArray(); 18 | 19 | for (char c : str_array){ 20 | 21 | if (namesMap.containsKey(c)){ 22 | 23 | namesMap.put(c, namesMap.get(c) + 1); 24 | 25 | }else{ 26 | 27 | namesMap.put(c, 1); 28 | 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | System.out.println(namesMap); 36 | 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /Lecture 58/Question17.java: -------------------------------------------------------------------------------- 1 | Create a class WordcountDemo with a main method. The program takes input from the command line arguments. Write a program to count the number of words in the given input, print the result as shown in the example. Fill the missing code in the below program. 2 | 3 | Sample Input and output: 4 | Cmd Args : This is a good day 5 | {a=1, This=1, is=1, good=1, day=1} 6 | 7 | 8 | 9 | package q24101; 10 | import java.util.*; 11 | public class WordcountDemo { 12 | public static void main(String[] args) { 13 | HashMap ans = new HashMap(); 14 | // write your code here 15 | // Take input from the command line argument 16 | // Find the occurrence of each word in the given string 17 | 18 | 19 | for (int i=0;i namesMap = new TreeMap(); 14 | // Write your code here 15 | for(int i=0;i nameSet = namesMap.keySet(); 18 | for (Object key : nameSet) { 19 | // Write your code here 20 | System.out.println(key+" : "+namesMap.get(key)); 21 | 22 | 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Lecture 58/Question19.java: -------------------------------------------------------------------------------- 1 | boolean containsKey(Object key): Which returns true if this map contains a mapping for the specified key 2 | 3 | Fill the missing code in the below program. Follow the instructions given in the comment lines. 4 | 5 | Sample Input and Output: 6 | {Ban=Bangalre, Hy=Hyderabad, Ke=Kerala} 7 | true 8 | 9 | 10 | package q24090; 11 | import java.util.*; 12 | public class TreeMapDemo { 13 | public static void main(String[] args) { 14 | Map namesMap = new TreeMap(); 15 | namesMap.put("Hy", "Hyderabad"); 16 | namesMap.put("Ban", "Bangalre"); 17 | namesMap.put("Ke","Kerala"); 18 | System.out.println(namesMap); 19 | 20 | // Find whether the given map contains the key Ban or not. 21 | System.out.println(namesMap.containsKey("Ban")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Lecture 59/Question03.java: -------------------------------------------------------------------------------- 1 | See and retype the below code to familiarize yourself with some of the commonly used methods in HashSet. 2 | 3 | The class HashSetMethodsDemo iterates through all the arguments passed to the main method, and stores them into an HashSet, which is later manipulated using its methods. 4 | 5 | Correlate the code and output to understand the usage of the methods. 6 | 7 | 8 | 9 | package q11380; 10 | import java.util.*; 11 | public class HashSetMethodsDemo { 12 | public static void main(String[] args) { 13 | Set aSet = new HashSet(args.length); 14 | for (String argument : args) { 15 | aSet.add(argument); 16 | } 17 | System.out.println("aSet = " + aSet); 18 | System.out.println("aSet.size() = " + aSet.size()); 19 | boolean removedFlag = aSet.remove("Larry Page"); 20 | System.out.println("Larry Page removedFlag = " + removedFlag); 21 | System.out.println("aSet = " + aSet); 22 | aSet.add("Steve Jobs"); 23 | System.out.println("aSet = " + aSet); 24 | aSet.add("Bill Gates"); 25 | System.out.println("aSet = " + aSet); 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture 59/Question04.java: -------------------------------------------------------------------------------- 1 | See the code and retype the same to learn how to iterate over the elements stored in a HashSet. 2 | 3 | Note the usage of HashSet class and the for-each statement. 4 | 5 | The class scans through all the arguments passed to the main method, and stores them into a HashSet if the argument's first char is in uppercase. 6 | 7 | The program first uses the for-each loop to print all the stored names from the HashSet one name on each line. 8 | 9 | Note that HashSet does not maintain the any order while iterating on the elements. If we want to have the insertion order to be maintained during iteration, we should use LinkedHashSet, about which we will learn later. 10 | 11 | 12 | 13 | package q11381; 14 | import java.util.*; 15 | public class HashSetIterationDemo { 16 | public static void main(String[] args) { 17 | Set namesSet = new HashSet(); 18 | for (String argument : args) { 19 | if (Character.isUpperCase(argument.charAt(0))) { 20 | namesSet.add(argument); 21 | } 22 | } 23 | for (Object name : namesSet) { 24 | System.out.println(name); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Lecture 59/Question06.java: -------------------------------------------------------------------------------- 1 | The LinkedHashSet is a subclass of HashSet. 2 | 3 | Unlike a HashSet which does not maintain order of the added entries, a LinkedHashSet orders the entries in their insertion order by default. 4 | 5 | See and retype the below code. You will notice that the entries in the LinkedHashSet always maintain their insertion order. 6 | 7 | 8 | package q11383; 9 | import java.util.*; 10 | public class LinkedHashSetDemo { 11 | public static void main(String[] args) { 12 | Set namesSet = new LinkedHashSet(); 13 | namesSet.add("Samos"); 14 | namesSet.add("Hyderabad"); 15 | namesSet.add("Dallas"); 16 | namesSet.add("Dallas"); 17 | System.out.println("namesSet = " + namesSet); 18 | namesSet.add("Dallas"); 19 | namesSet.add("Bangalore"); 20 | System.out.println("namesSet = " + namesSet); 21 | namesSet.add("Athens"); 22 | System.out.println("namesSet = " + namesSet); 23 | } 24 | } -------------------------------------------------------------------------------- /Lecture 59/Question08.java: -------------------------------------------------------------------------------- 1 | Write a program to understand the working of the method contains. Create a class SetDemo with a main method. Create a set instance and add the given elements to the set. 2 | 3 | Add the following elements to the set: 4 | Ganga 5 | Krishna 6 | Godavari 7 | Yamuna 8 | Find if the list contains the elements Godavari and Sindhu if the element present in the list it returns true else returns false and observe the output. 9 | 10 | 11 | 12 | package q11976; 13 | import java.util.*; 14 | public class SetDemo { 15 | public static void main(String[] args) { 16 | Set namesSet = new HashSet (); 17 | 18 | // write your code here 19 | namesSet.add("Ganga"); 20 | 21 | namesSet.add("Krishna"); 22 | 23 | namesSet.add("Godavari"); 24 | 25 | namesSet.add("Yamuna"); 26 | 27 | 28 | 29 | 30 | 31 | System.out.println(namesSet); 32 | 33 | // write your code here 34 | System.out.println(namesSet.contains("Godavari")); 35 | System.out.println(namesSet.contains("Sindhu")); 36 | 37 | 38 | 39 | 40 | } 41 | } 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Lecture 59/Question09.java: -------------------------------------------------------------------------------- 1 | Write a program to understand how to remove an element in the Set using the remove method. Create a class SetDemo with a main method and insert the numbers from one to five and remove the elements two and three from the set and print the result. 2 | 3 | 4 | 5 | import java.util.*; 6 | public class SetDemo { 7 | public static void main(String[] args) { 8 | Set namesSet = new HashSet (); 9 | 10 | 11 | // write your code here 12 | namesSet.add("Five"); 13 | 14 | namesSet.add("One"); 15 | 16 | namesSet.add("Four"); 17 | 18 | namesSet.add("Two"); 19 | 20 | namesSet.add("Three"); 21 | 22 | 23 | 24 | 25 | 26 | System.out.println(namesSet); 27 | 28 | // write your code here 29 | namesSet.remove("Two"); 30 | namesSet.remove("Three"); 31 | 32 | System.out.println(namesSet); 33 | } 34 | } -------------------------------------------------------------------------------- /Lecture 59/Question10.java: -------------------------------------------------------------------------------- 1 | Write a program to understand the Set methods clear and size. Create a class SetDemo with a main method and insert the days of week from Sunday through Saturday. 2 | 3 | 4 | 5 | import java.util.*; 6 | public class SetDemo { 7 | public static void main(String[] args) { 8 | Set namesSet = new HashSet (); 9 | 10 | // add days of week to the set using the method add 11 | namesSet.add("Monday"); 12 | 13 | namesSet.add("Thursday"); 14 | 15 | namesSet.add("Friday"); 16 | 17 | namesSet.add("Sunday"); 18 | 19 | namesSet.add("Wednesday"); 20 | 21 | namesSet.add("Tuesday"); 22 | 23 | namesSet.add("Saturday"); 24 | 25 | 26 | 27 | 28 | 29 | System.out.println(namesSet); 30 | System.out.println("Size of the set is " + namesSet.size());// print the size of the set 31 | 32 | // clear all elements in the set 33 | namesSet.clear(); 34 | 35 | System.out.println("The set after removing all the elements " + namesSet); 36 | System.out.println("Size of the set after removing the elements is " + namesSet.size()); 37 | } 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture 59/Question11.java: -------------------------------------------------------------------------------- 1 | Create a class SetDemo with a main method. The method takes inputs from the command line arguments. Create a set instance and add these inputs to the set. Fill the missing code in the below program and print the output as shown in the example. 2 | 3 | Sample Input and Output: 4 | Cmd Args : Hyderabad Kolkata Hyderabad Delhi Mumbai 5 | Delhi 6 | Kolkata 7 | Mumbai 8 | Hyderabad 9 | From the example we can observe that the set does not allow duplicate elements, also does not maintain the insertion order 10 | 11 | 12 | 13 | package q24095; 14 | import java.util.*; 15 | public class SetDemo { 16 | public static void main(String[] args) { 17 | Set namesSet = new HashSet (); 18 | // write your code here 19 | 20 | // iterate over the set using iterator and print the output 21 | for(int i=0;i iterate = namesSet.iterator(); 24 | 25 | 26 | 27 | while(iterate.hasNext()) { 28 | 29 | System.out.println(iterate.next()); 30 | 31 | } 32 | 33 | 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /Lecture 60/Question4.java: -------------------------------------------------------------------------------- 1 | Given : 2 | public class Test{ 3 | public static void main(String[] args) { 4 | String myProp = /* insert code here */ 5 | System.out.println(myProp); 6 | } 7 | } 8 | and the command line: 9 | java -Dprop.custom=gobstopper Test 10 | Which two code snippets when placed at String myProp = /* insert code here */, will produce the output gobstopper? (Choose two.) 11 | 12 | 13 | 14 | Answer 15 | 16 | 17 | System.getProperty("prop.custom"); 18 | System.getProperties().getProperty("prop.custom"); -------------------------------------------------------------------------------- /Lecture 61/Question2.java: -------------------------------------------------------------------------------- 1 | All the collections in Java are parameterized using generic syntax. For example when we see the List interface we will find : 2 | public interface List extends Collection{ 3 | public boolean add(E e); 4 | 5 | public E get(int index); 6 | ... 7 | ... 8 | } 9 | In the above example, List is called generic interface. Similarly we can have generic classes. The E surrounded by < and > is called the type parameter. 10 | 11 | In the below code: 12 | List namesList = new ArrayList(); 13 | String is called type argument passed to List and ArrayList. 14 | 15 | Any class or interface which accepts parameterized types is called a generic class or a generic interface respectively. Select all the correct statements for the below code: 16 | class A { // statement 1 17 | } 18 | 19 | class B { // statement 2 20 | } 21 | 22 | B b1 = new B(); // statement 3 23 | 24 | B b1 = new B(); // statement 4 25 | 26 | 27 | 28 | Answer 29 | 30 | 31 | In statement 1, class A is called a non-generic class. 32 | In statement 2, class B is called a generic class. -------------------------------------------------------------------------------- /Lecture 61/Question5.java: -------------------------------------------------------------------------------- 1 | Below code shows how to use generics while using a Map. Observe how we iterate only keys of the Map. 2 | 3 | 4 | 5 | package q11392; 6 | import java.util.*; 7 | public class SimpleMapDemo { 8 | public static void main(String[] args) { 9 | Map countryCodesMap = new HashMap(); 10 | countryCodesMap.put("IN", "India"); 11 | countryCodesMap.put("CA", "Canada"); 12 | countryCodesMap.put("AG", "Argentina"); 13 | countryCodesMap.put("BR", "Brazil"); 14 | Set codesSet = countryCodesMap.keySet(); 15 | for (String code : codesSet) { 16 | String countryName = countryCodesMap.get(code); 17 | System.out.println(code + " is the code for : " + countryName); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Lecture 65/Question05.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | import java.util.function.Predicate; 4 | 5 | class PredicateInterface{ 6 | 7 | public static void main(String args[]){ 8 | 9 | List Words = Arrays.asList("Ready","To","Visit","Coders","Den","CodeTantriks"); 10 | 11 | Predicate P = (s)->s.startsWith("C"); 12 | 13 | for (String W:Words){ 14 | 15 | if (P.test(W)){ 16 | 17 | System.out.println(W); 18 | 19 | } 20 | 21 | 22 | } 23 | 24 | 25 | } 26 | 27 | 28 | 29 | } 30 | 31 | 32 | 33 | 34 | Answer 35 | 36 | import java.util.*; 37 | import java.lang.*; 38 | import java.io.*; 39 | 40 | interface FuncInterface { 41 | void AbstractFunction(int A); 42 | 43 | default void GeneralFunction() { 44 | System.out.println("Welcome"); 45 | 46 | } 47 | 48 | } 49 | 50 | class LambdaExpression { 51 | public static void main(String args[]) { 52 | 53 | FuncInterface fobj = (int A) -> System.out.println(10 + A); 54 | 55 | fobj.AbstractFunction(15); 56 | 57 | } 58 | 59 | } 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture 65/Question07.java: -------------------------------------------------------------------------------- 1 | Complete the code in LambdaExpression.java file to print all odd elements in the array list using concept of lambda expression 2 | 3 | Sample Test Cases 4 | Test Case 1: 5 | Expected Output: 6 | 120 7 | 303 8 | 308 9 | 555 10 | All·odd·elements·displayed 11 | 303 12 | 555 13 | 14 | 15 | 16 | Answer 17 | 18 | // Complete below given code as per the problem statement 19 | import java.util.ArrayList; 20 | class LambdaExpression{ 21 | public static void main(String args[]){ 22 | ArrayList ArrList = new ArrayList(); 23 | ArrList.add(120); 24 | ArrList.add(303); 25 | ArrList.add(308); 26 | ArrList.add(555); 27 | //Complete the code for displaying elements using lambda expression 28 | ArrList.forEach(n -> System.out.println(n)); 29 | 30 | System.out.println("All odd elements displayed"); 31 | ArrList.forEach(n-> { if (n%2 != 0) System.out.println(n); }); 32 | 33 | // Complete the Lambda 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codetantra CSE310 - PROGRAMMING IN JAVA Answers 2 | This Repository contanis all codes and mcq of codetantra java programming course. 3 | 4 | ### Question format- 5 | Problem Statment
6 | Solution

7 | Here are all the completed and successful Code for Codetantra JAVA Programming Course 8 |
9 | 10 | Fork and Give A Star to repository. 11 |
12 |
13 | Keep Checking For further updates. 14 |
15 |
16 | ## Happy Coding!! 17 | -------------------------------------------------------------------------------- /sciptt.py: -------------------------------------------------------------------------------- 1 | import pyautogui ,time 2 | time.sleep(5) 3 | f=open('bee.txt','r') 4 | for word in f: 5 | pyautogui.typewrite(word) 6 | pyautogui.press('enter') 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 | --------------------------------------------------------------------------------