├── Lecture 02
├── Question4.java
├── Question6.java
├── Question3.java
├── Question5.java
└── Question1.java
├── Lecture 05
├── Question05.java
└── Question02.java
├── Lecture 01
├── Question2.java
└── Question4.java
├── Lecture 03
├── Question06.java
├── Question07.java
├── Question04.java
├── Question08.java
├── Question10.java
├── Question11.java
├── Question09.java
├── Question02.java
├── Question12.java
├── Question05.java
└── Question03.java
├── Lecture 04
├── Question01.java
├── Question02.java
├── Question03.java
├── Question04.java
├── Question05.java
├── Question06.java
├── Question07.java
├── Question08.java
├── Question09.java
├── Question11.java
├── Question12.java
├── Question13.java
├── Question14.java
├── Question17.java
├── Question18.java
├── Question16.java
├── Question20.java
├── Question10.java
└── Question15.java
├── Lecture 08
├── Question02.java
├── Question06.java
├── Question10.java
├── Question03.java
├── Question01.java
└── Question05.java
├── Lecture 15
├── Question18.java
├── Question19.java
├── Question02.java
├── Question15.java
├── Question12.java
├── Question03.java
├── Question08.java
├── Question11.java
├── Question07.java
├── Question17.java
├── Question05.java
├── Question06.java
├── Question16.java
├── Question04.java
├── Question01.java
├── Question10.java
├── Question09.java
└── Question14.java
├── Lecture 07
├── Question09.java
├── Question12.java
├── Question04.java
├── Question08.java
├── Question11.java
└── Question01.java
├── Lecture 24
├── Question3.java
├── Question4.java
├── Question5.java
└── Question2.java
├── Lecture 06
├── Question09.java
├── Question11.java
├── Question05.java
├── Question10.java
├── Question04.java
├── Question02.java
├── Question08.java
└── Question03.java
├── Lecture 23
├── Question8.java
├── Question7.java
└── Question6.java
├── sciptt.py
├── Lecture 31
├── Question26.java
├── Question05.java
├── Question06.java
├── Question04.java
├── Question12.java
├── Question18.java
├── Question09.java
├── Question07.java
├── Question21.java
├── Question14.java
├── Question16.java
├── Question17.java
├── Question08.java
├── Question20.java
├── Question01.java
├── Question10.java
├── Question24.java
├── Question02.java
├── Question22.java
├── Question03.java
├── Question19.java
├── Question15.java
├── Question11.java
├── Question13.java
└── Question25.java
├── Lecture 45
├── Question3.java
├── Question4.java
├── Question1.java
└── Question2.java
├── Lecture 44
├── Question11.java
├── Question06.java
├── Question12.java
├── Question07.java
├── Question10.java
├── Question08.java
└── Question02.java
├── Lecture 51
├── Question4.java
└── Question2.java
├── Lecture 43
├── Question1.java
└── Question2.java
├── Lecture 46
├── Question09.java
└── Question11.java
├── Lecture 27
├── Question5.java
├── Question4.java
├── Question6.java
├── Question3.java
└── Question1.java
├── Lecture 30
├── Question3.java
├── Question4.java
└── Question5.java
├── Lecture 12
├── Question3.java
├── Question5.java
├── Question6.java
└── Question2.java
├── Lecture 32
├── Question6.java
└── Question2.java
├── Lecture 11
├── Question5.java
├── Question2.java
├── Question1.java
└── Question6.java
├── Lecture 47
└── Question4.java
├── Lecture 17
├── Question09.java
├── Question08.java
├── Question06.java
├── Question04.java
├── Question02.java
├── Question11.java
├── Question05.java
└── Question03.java
├── Lecture 21
├── Question05.java
├── Question10.java
├── Question18.java
├── Question04.java
├── Question24.java
├── Question02.java
├── Question15.java
├── Question07.java
├── Question09.java
├── Question13.java
├── Question21.java
├── Question01.java
├── Question08.java
├── Question26.java
├── Question11.java
├── Question14.java
├── Question17.java
├── Question22.java
├── Question25.java
├── Question28.java
├── Question16.java
└── Question03.java
├── README.md
├── Lecture 28
├── Question03.java
├── Question02.java
├── Question07.java
├── Question05.java
├── Question04.java
├── Question10.java
├── Question09.java
├── Question08.java
└── Question11.java
├── Lecture 29
├── Question03.java
├── Question10.java
├── Question11.java
├── Question04.java
├── Question02.java
├── Question07.java
├── Question08.java
├── Question14.java
├── Question05.java
├── Question13.java
└── Question12.java
├── Lecture 60
└── Question4.java
├── Lecture 10
└── Question3.java
├── Lecture 25
├── Question15.java
├── Question05.java
├── Question06.java
├── Question02.java
├── Question04.java
└── Question09.java
├── Lecture 19
├── Question1.java
├── Question2.java
├── Question7.java
├── Question5.java
├── Question9.java
├── Question3.java
├── Question8.java
├── Question6.java
└── Question4.java
├── Lecture 55
└── Question5.java
├── Lecture 57
├── Question07.java
├── Question18.java
├── Question08.java
├── Question11.java
├── Question16.java
├── Question19.java
├── Question17.java
├── Question06.java
├── Question04.java
└── Question14.java
├── Lecture 16
├── Question6.java
├── Question8.java
└── Question7.java
├── Lecture 18
├── Question3.java
├── Question4.java
└── Question6.java
├── Lecture 61
├── Question5.java
└── Question2.java
├── Lecture 52
└── Question2.java
├── Lecture 37
├── Question6.java
├── Question7.java
└── Question9.java
├── Lecture 58
├── Question19.java
├── Question18.java
├── Question17.java
├── Question10.java
├── Question15.java
└── Question12.java
├── Lecture 09
├── Question3.java
├── Question4.java
├── Question2.java
└── Question5.java
├── Lecture 59
├── Question09.java
├── Question06.java
├── Question11.java
├── Question03.java
├── Question10.java
├── Question08.java
└── Question04.java
├── Lecture 14
├── Question5.java
├── Question4.java
└── Question3.java
├── Lecture 65
├── Question07.java
└── Question05.java
├── Lecture 22
├── Question12.java
├── Question03.java
├── Question10.java
├── Question11.java
├── Question04.java
└── Question05.java
├── Lecture 48
└── Question2.java
├── Lecture 56
├── Question2.java
├── Question4.java
└── Question6.java
├── Lecture 42
└── Question3.java
├── Lecture 34
├── Question01.java
├── Question06.java
└── Question04.java
├── Lecture 35
└── Question1.java
└── Lecture 26
└── Question9.java
/Lecture 02/Question4.java:
--------------------------------------------------------------------------------
1 | package q10751;
2 | public class Student {
3 |
4 | }
--------------------------------------------------------------------------------
/Lecture 02/Question6.java:
--------------------------------------------------------------------------------
1 | package q11602;
2 | public class Test {
3 |
4 | }
--------------------------------------------------------------------------------
/Lecture 02/Question3.java:
--------------------------------------------------------------------------------
1 | package q10737;
2 | public class Student {
3 |
4 | }
--------------------------------------------------------------------------------
/Lecture 02/Question5.java:
--------------------------------------------------------------------------------
1 | package q10754;
2 | public class Student {
3 |
4 | }
--------------------------------------------------------------------------------
/Lecture 05/Question05.java:
--------------------------------------------------------------------------------
1 | Select all the correct answers
2 | Answer
3 |
4 | In Binary System, decimal 3 is represented as 0b11
--------------------------------------------------------------------------------
/Lecture 01/Question2.java:
--------------------------------------------------------------------------------
1 | public class FirstProgram {
2 | public static void main(String[] args) {
3 | System.out.println("Hello Java");
4 | }
5 | }
--------------------------------------------------------------------------------
/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/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/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/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 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 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 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 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 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 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 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 24/Question3.java:
--------------------------------------------------------------------------------
1 | Identify the errors in the given code and correct them.
2 |
3 |
4 | package q11159;
5 | public class Student {
6 | private String id;
7 | private String name;
8 | private int age;
9 | private char gender;
10 | }
11 |
12 |
--------------------------------------------------------------------------------
/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/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/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 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 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/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 23/Question8.java:
--------------------------------------------------------------------------------
1 | Identify the errors in below code and correct them.
2 |
3 | Note: Please don't change the package name.'
4 |
5 |
6 | package q11217;
7 | public class Student {
8 | private String id;
9 | private String name;
10 | private int age;
11 | private char gender;
12 | }
13 |
14 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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 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 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 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 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 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 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 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 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 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 24/Question4.java:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | package q11160;
6 | public class Student {
7 | private String id;
8 | private String name;
9 | private int age;
10 | private char gender;
11 |
12 | public Student(String id, String name, int age, char gender) {
13 | this.id = id;
14 | this.name = name;
15 | this.age = age;
16 | this.gender = gender;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Lecture 27/Question5.java:
--------------------------------------------------------------------------------
1 | Fix the text in the main method using escape characters to produce the below output.
2 |
3 | There are double-quotes around "Red"
4 |
5 | package q11148;
6 | public class EscapeSequence {
7 | public static void main(String[] args) {
8 | String text1 = "There are double-quotes around \"Red\"";
9 | System.out.println(text1);
10 | }
11 | }
12 |
13 |
--------------------------------------------------------------------------------
/Lecture 30/Question3.java:
--------------------------------------------------------------------------------
1 | package q11187;
2 | public class StringFormatting {
3 | public static void main(String[] args) {
4 | float dVar = 3.141f;
5 |
6 | System.out.printf("%f%n", dVar);
7 | System.out.printf("%.3f%n", dVar);
8 | System.out.printf("%.2f%n", dVar);
9 | System.out.printf("%08.4f%n", dVar);
10 | System.out.printf("%+08.4f%n", dVar);
11 | }
12 | }
--------------------------------------------------------------------------------
/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 27/Question4.java:
--------------------------------------------------------------------------------
1 | Fix the text in the main method using an escape character to produce the below output.
2 |
3 | There is a single double-quote before "BLUE
4 |
5 |
6 | package q11147;
7 | public class EscapeSequence {
8 | public static void main(String[] args) {
9 | String text = "There is a single double-quote before \"BLUE";
10 | System.out.println(text);
11 | }
12 | }
--------------------------------------------------------------------------------
/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 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 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 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 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 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 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 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 31/Question05.java:
--------------------------------------------------------------------------------
1 | Write a class TestSubstring with a main method. The method receives one command line argument and print the first half of the argument.
2 |
3 | Here is an example:
4 | Cmd Args : Eight
5 | Ei
6 |
7 |
8 | package q11195;
9 | public class TestSubstring {
10 | public static void main(String[] args){
11 | // int n=args[0].length
12 | System.out.println(args[0].substring(0,args[0].length()/2));
13 | }
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/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 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 21/Question05.java:
--------------------------------------------------------------------------------
1 | package q11050;
2 | public class ElementCheck {
3 | /**
4 | * check if first or last elements of the arr1 and arr2 are same or not
5 | *
6 | *
7 | * @return result
8 | */
9 |
10 | public boolean checkFirstOrLast(int[] arr1, int[] arr2) {
11 | //Write your code here
12 | if(arr1[0]==arr2[0] || arr1[arr1.length-1]==arr2[arr2.length-1]){
13 | return true;
14 | }else{
15 | return false;
16 | }
17 | }
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/Lecture 31/Question06.java:
--------------------------------------------------------------------------------
1 | Write a class TestSubstring with a main method. The method receives one command line argument and prints the output by removing the first and last characters of the argument.
2 |
3 | For example:
4 | Cmd Args : January
5 | anuar
6 |
7 | package q11196;
8 |
9 |
10 | public class TestSubstring{
11 |
12 | public static void main(String[] args) {
13 |
14 | System.out.println(args[0].substring(1,args[0].length()-1));
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Lecture 28/Question03.java:
--------------------------------------------------------------------------------
1 | Create a class TestStringMethods with a main method. The method receives one command line argument. Print the 5th character in the argument.
2 |
3 | For Example:
4 | Cmd Args : Strange
5 | n
6 |
7 |
8 | package q11153;
9 | public class TestStringMethods {
10 |
11 |
12 |
13 | public static void main(String[] args) {
14 |
15 |
16 |
17 | String str = args[0];
18 |
19 |
20 |
21 | System.out.println(str.charAt(4));
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/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 29/Question03.java:
--------------------------------------------------------------------------------
1 | Write a class StringIndexOf with a main method. The method receives one command line argument. Print the second occurrence of the string CA in the argument.
2 |
3 | For Example:
4 | Cmd Args : CALIFORNICA
5 | 9
6 |
7 | package q11173;
8 |
9 |
10 | public class StringIndexOf {
11 |
12 | public static void main(String[] args) {
13 |
14 | String str = args[0];
15 |
16 | System.out.println(str.indexOf("CA",2));
17 |
18 |
19 |
20 |
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/Lecture 29/Question10.java:
--------------------------------------------------------------------------------
1 | Write a class StringTrim with a main method. The method receives one command line argument, remove any leading or trailing white spaces from the argument and print the argument.
2 |
3 | For Example:
4 | Cmd Args : blank
5 | blank
6 |
7 |
8 |
9 | package q11180;
10 |
11 |
12 | public class StringTrim{
13 |
14 | public static void main(String[] args) {
15 |
16 | String str = args[0];
17 |
18 | System.out.println(str.trim());
19 |
20 |
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/Lecture 31/Question04.java:
--------------------------------------------------------------------------------
1 | Write a class TestSubstring with a main method. The method receives one command line argument and print the output formed by the first three characters of the argument.
2 |
3 | For example:
4 | Cmd Args : Important
5 | Imp
6 |
7 |
8 | package q11194;
9 |
10 |
11 | public class TestSubstring{
12 |
13 | public static void main(String[] args) {
14 |
15 | System.out.println(args[0].substring(0,3));
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | }
25 | }
--------------------------------------------------------------------------------
/Lecture 31/Question12.java:
--------------------------------------------------------------------------------
1 | Write a class ReverseString with a main method. The method receives one command line argument. Write a logic to reverse it and print the output.
2 |
3 | For Example:
4 | Cmd Args : Ram lakshman
5 | namhskal maR
6 |
7 | package q11202;
8 | public class ReverseString{
9 | public static void main(String[] args){
10 | String s=args[0];
11 | String ans="";
12 | for(int i=s.length()-1;i>=0;i--){
13 | ans+=s.charAt(i);
14 | }
15 | System.out.println(ans.trim());
16 | }
17 | }
--------------------------------------------------------------------------------
/Lecture 29/Question11.java:
--------------------------------------------------------------------------------
1 | Create a class StringReplace with a main method. The method receives one command line argument , replace all the occurrences of character c with k and print the result.
2 |
3 | For Example:
4 | Cmd Args : Acceptance
5 | Akkeptanke
6 |
7 |
8 | package q11181;
9 |
10 |
11 | public class StringReplace{
12 |
13 | public static void main(String[] args) {
14 |
15 | String str = args[0];
16 |
17 | System.out.println(str.replace("c", "k"));
18 |
19 |
20 | }
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/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 28/Question02.java:
--------------------------------------------------------------------------------
1 | Create a class TestStringMethods with a main method. The method receives one command line argument. Calculate the length of the argument and print the output.
2 |
3 | For Example:
4 | Cmd Args : Independance
5 | 12
6 |
7 | package q11152;
8 |
9 |
10 | public class TestStringMethods {
11 |
12 | public static void main(String[] args) {
13 |
14 | String str = args[0];
15 |
16 | System.out.println(str.length());
17 |
18 |
19 |
20 | }
21 |
22 |
23 |
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/Lecture 28/Question07.java:
--------------------------------------------------------------------------------
1 | Write a class StringCompare with a main method. The method receives two command line arguments and prints true if both are equal.
2 |
3 | For Example:
4 | Cmd Args : India India
5 | true
6 |
7 |
8 |
9 |
10 |
11 |
12 | package q11165;
13 |
14 |
15 | public class StringCompare {
16 |
17 | public static void main(String[] args) {
18 |
19 | String str1 = args[0];
20 |
21 | String str2 = args[1];
22 |
23 | System.out.println(str1.equals(str2));
24 |
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/Lecture 29/Question04.java:
--------------------------------------------------------------------------------
1 | Write a class StringLastIndexOf with a main method. The method receives one command line argument. Print the index of the last occurrence of the character * in the argument.
2 |
3 | For Example:
4 | Cmd Args : TCA*TX*TX*
5 | 9
6 |
7 |
8 | package q11174;
9 |
10 |
11 | public class StringLastIndexOf {
12 |
13 | public static void main(String[] args) {
14 |
15 | String str = args[0];
16 |
17 | System.out.println(str.lastIndexOf("*"));
18 |
19 |
20 |
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/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 28/Question05.java:
--------------------------------------------------------------------------------
1 | Create a class TestStringMethods with a main method. The method receives one command line argument. Convert the argument to uppercase and print the result.
2 |
3 | For Example:
4 | Cmd Args : India
5 | INDIA
6 |
7 | package q11163;
8 | public class TestStringMethods {
9 |
10 |
11 |
12 | public static void main(String[] args) {
13 |
14 |
15 |
16 | String str = args[0];
17 |
18 |
19 |
20 | System.out.println(str.toUpperCase());
21 |
22 |
23 |
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Lecture 29/Question02.java:
--------------------------------------------------------------------------------
1 | Write a class StringIndexOf with a main method. The method receives one command line argument. Print the index of the first occurrences of the string CA in the argument.
2 |
3 | For Example:
4 | Cmd Args : TCA*CX*CX*CAT
5 | 1
6 |
7 | package q11172;
8 |
9 |
10 | public class StringIndexOf {
11 |
12 | public static void main(String[] args) {
13 |
14 | String str = args[0];
15 |
16 | System.out.println(str.indexOf("CA"));
17 |
18 |
19 |
20 |
21 | }
22 | }
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/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 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 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 25/Question15.java:
--------------------------------------------------------------------------------
1 | The list of variables/references that are present in the method declaration are called parameters.
2 |
3 | When the method is invoked the actual values passed are called arguments.
4 |
5 | Select all the correct statements for the below code:
6 | public int sum(int num1, int num2) { // statement 1
7 | return num1 + num2;
8 | }
9 |
10 | int total = sum(2, 3); // statement 2
11 |
12 |
13 | Answers
14 | In statement 1, num1 and num2 are called parameters.
15 | In statement 2, values 2 and 3 are called arguments.
16 |
--------------------------------------------------------------------------------
/Lecture 31/Question09.java:
--------------------------------------------------------------------------------
1 | Write a class ExtractTag with a main method. The method receives one command line argument in between tags like [ ], extract the argument between the tags and print the output.
2 |
3 | For Example:
4 | Cmd Args : [Independent]
5 | Independent
6 |
7 |
8 | package q11199;
9 |
10 |
11 |
12 | public class ExtractTag{
13 |
14 | public static void main(String[] args) {
15 |
16 | String str=args[0].replace("[","").replace("]","");
17 |
18 | System.out.println(str);
19 |
20 |
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/Lecture 19/Question1.java:
--------------------------------------------------------------------------------
1 | Retype the code below. The class RiverNamePrinter prints an array containing the names of Indian rivers.
2 |
3 | The code in the main method uses a for-each loop to iterate over the array namesArr and prints each name.
4 |
5 | package q10936;
6 | public class RiverNamePrinter {
7 | public static void main(String[] args) {
8 | String[] namesArr = { "Ganga", "Yamuna", "Godavari", "Krishna", "Narmada", "Kaveri" };
9 | for (String river : namesArr) {
10 | System.out.println(river);
11 | }
12 | }
13 | }
14 |
15 |
16 |
--------------------------------------------------------------------------------
/Lecture 19/Question2.java:
--------------------------------------------------------------------------------
1 | Create a class MountainNamePrinter with a main method. Create an array with the following names: Nanda Devi, Kamet, K12, Dunagiri.
2 |
3 | Iterate over the array and print each name on a separate line.
4 |
5 |
6 | package q10937;
7 |
8 | public class MountainNamePrinter {
9 |
10 | public static void main(String[] args) {
11 |
12 | String[] namesArr = { "Nanda Devi", "Kamet", "K12", "Dunagiri"};
13 |
14 | for (String river : namesArr) {
15 |
16 | System.out.println(river);
17 |
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/Lecture 28/Question04.java:
--------------------------------------------------------------------------------
1 | Create a class TestStringMethods with a main method. The method receives one command line argument. Convert the argument to lowercase and print the output.
2 |
3 | For Example:
4 | Cmd Args : KRISHNA
5 | krishna
6 |
7 |
8 | package q11162;
9 | public class TestStringMethods {
10 |
11 |
12 |
13 | public static void main(String[] args) {
14 |
15 |
16 |
17 | String str = args[0];
18 |
19 |
20 |
21 | System.out.println(str.toLowerCase());
22 |
23 |
24 |
25 |
26 | }
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/Lecture 28/Question10.java:
--------------------------------------------------------------------------------
1 | Write a class StringCompare with a main method. The method receives one command line argument. Print true if the argument ends with the suffix ed, else print false.
2 |
3 | For Example:
4 | Cmd Args : prefixed
5 | true
6 |
7 |
8 | package q11168;
9 |
10 |
11 | public class StringCompare {
12 |
13 | public static void main(String[] args) {
14 |
15 | String str1 = args[0];
16 |
17 |
18 |
19 | System.out.println(str1.endsWith("ed"));
20 |
21 |
22 |
23 |
24 |
25 |
26 | }
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/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 28/Question09.java:
--------------------------------------------------------------------------------
1 | Write a class StringCompare witha a main method. The method receives one command line argument. Print true if the argument starts with the prefix pre, otherwise print false.
2 |
3 | For Example:
4 | Cmd Args : preparation
5 | true
6 |
7 |
8 | package q11167;
9 |
10 |
11 |
12 |
13 | public class StringCompare {
14 |
15 | public static void main(String[] args) {
16 |
17 | String str1 = args[0];
18 |
19 | System.out.println(str1.startsWith("pre"));
20 |
21 |
22 |
23 |
24 |
25 |
26 | }
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/Lecture 29/Question07.java:
--------------------------------------------------------------------------------
1 | Write a class SubstringText with a main method. The method receives one command line argument. Print a substring of the argument that starts from index 7.
2 |
3 | For Example:
4 | Cmd Args : Synchronicity
5 | nicity
6 | Note: Assume that the first argument will always contain more than 7 characters.
7 |
8 |
9 | package q11177;
10 |
11 |
12 | public class SubstringText {
13 |
14 | public static void main(String[] args) {
15 |
16 | String str = args[0];
17 |
18 | System.out.println(str.substring(7));
19 |
20 | }
21 | }
--------------------------------------------------------------------------------
/Lecture 31/Question07.java:
--------------------------------------------------------------------------------
1 | Write a class StringStairs with a main method. The method receives one command line argument and prints the first char in one line, then the first two chars in the next line, etc.
2 |
3 | For example:
4 | Cmd Args : Delhi
5 | D
6 | De
7 | Del
8 | Delh
9 | Delhi
10 |
11 |
12 | package q11197;
13 |
14 |
15 | public class StringStairs{
16 |
17 | public static void main(String[] args) {
18 |
19 | for(int i=1;i<=args[0].length();i++){
20 |
21 | System.out.println(args[0].substring(0,i));
22 |
23 |
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/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 24/Question5.java:
--------------------------------------------------------------------------------
1 | A constructor must be written inside the class body (i.e. inside the open brace { and the closing brace } of the class body).
2 |
3 | Identify the errors and correct them.
4 |
5 |
6 | package q11161;
7 | public class Student {
8 | private String id;
9 | private String name;
10 | private int age;
11 | private char gender;
12 |
13 |
14 |
15 | public Student(String name, String rollNo, int age, char gender) {
16 | this.id = id;
17 | this.name = name;
18 | this.age = age;
19 | this.gender = gender;
20 | }
21 | }
22 |
23 |
24 |
--------------------------------------------------------------------------------
/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/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 27/Question6.java:
--------------------------------------------------------------------------------
1 | Fix the text in the main method using escape characters to produce the below output.
2 |
3 | insert into Employee(id, name) values("10","Meka");
4 | insert into Employee(id, name) values("11","Reka");
5 |
6 |
7 | package q11149;
8 | public class EscapeSequence {
9 | public static void main(String[] args) {
10 | String text1 = "insert into Employee(id, name) values(\"10\",\"Meka\");";
11 | String text2 = "insert into Employee(id, name) values(\"11\",\"Reka\");";
12 | System.out.println(text1);
13 | System.out.println(text2);
14 | }
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/Lecture 31/Question08.java:
--------------------------------------------------------------------------------
1 | Write a class RemoveChar with a main method. The method receives one command line argument and prints the output by removing the 3rd character from the argument.
2 |
3 | Assumptions:
4 | Length of the argument is greater than or equal to 3 characters
5 | For example:
6 | Cmd Args : Victory
7 | Vitory
8 |
9 |
10 | package q11198;
11 |
12 |
13 | public class RemoveChar{
14 |
15 | public static void main(String[] args) {
16 |
17 | String str=args[0];
18 |
19 | System.out.println(str.substring(0, 2) + str.substring(2+1));
20 |
21 |
22 | }
23 | }
--------------------------------------------------------------------------------
/Lecture 28/Question08.java:
--------------------------------------------------------------------------------
1 | Write a class StringCompare with a main method. The method receives two command line arguments and prints true if both are equal.
2 |
3 | Assumptions:
4 | Ignoring the case of the two arguments
5 | For Example:
6 | Cmd Args : Congrats conGrats
7 | true
8 |
9 |
10 | package q11166;
11 |
12 |
13 | public class StringCompare {
14 |
15 | public static void main(String[] args) {
16 |
17 | String str1 = args[0];
18 |
19 | String str2 = args[1];
20 |
21 | System.out.println(str1.equalsIgnoreCase(str2));
22 |
23 |
24 |
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/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 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 31/Question01.java:
--------------------------------------------------------------------------------
1 | Write a class Greeting with a main method. The method receives one command line argument and prints the output as shown below.
2 |
3 | For example:
4 | Cmd Args : Ram
5 | Hello Ram!
6 | Note: Make sure to use println and not print method. If you want to use printf, ensure to print a \n character at the end.
7 |
8 |
9 |
10 | package q11191;
11 |
12 |
13 | public class Greeting{
14 |
15 | public static void main(String[] args) {
16 |
17 | String ans=args[0];
18 |
19 |
20 |
21 | System.out.println("Hello "+ ans +"!");
22 |
23 |
24 |
25 |
26 | }
27 | }
--------------------------------------------------------------------------------
/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 31/Question10.java:
--------------------------------------------------------------------------------
1 | Write a class AttachTag with a main mehod. The method receives two command line arguments make the second argument as a special tag and attach before and after the first argument and print the value.
2 |
3 | For example:
4 | Cmd Args : Delhi New
5 | [New]Delhi[/New]
6 | Note: Make sure to use println and not print method.
7 |
8 | package q11200;
9 |
10 |
11 | public class AttachTag{
12 |
13 | public static void main(String[] args) {
14 |
15 | String str="["+args[1]+"]"+args[0]+"[/"+args[1]+"]";
16 |
17 | System.out.println(str);
18 |
19 |
20 | }
21 | }
22 |
23 |
24 |
--------------------------------------------------------------------------------
/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 28/Question11.java:
--------------------------------------------------------------------------------
1 | Write a class StringCompare with a main the method. The method receives two command line arguments compare the first and second arguments using equals() method and also by using == operator and print the result.
2 |
3 | For example :
4 | Cmd Args : Ganga Ganga
5 | The result with equals : true
6 | The result with == : false
7 |
8 |
9 | package q11169;
10 | public class StringCompare {
11 | public static void main(String[] args) {
12 | System.out.println("The result with equals : " + args[0].equals(args[1]));
13 | System.out.println("The result with == : " + (args[0]==args[1]));
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Lecture 29/Question08.java:
--------------------------------------------------------------------------------
1 | Write a class SubstringText with a main method. The method receives one command line argument. Print the substring of the argument that starts at index 5 and ends at index 12 (should include the character at index 12).
2 |
3 | Assumptions:
4 | The argument contains more than 12 characters
5 | For Example:
6 | Cmd Args : Merry-go-round
7 | -go-roun
8 |
9 |
10 | package q11178;
11 |
12 |
13 | public class SubstringText {
14 |
15 | public static void main(String[] args) {
16 |
17 | String str = args[0];
18 |
19 | System.out.println(str.substring(5,13));
20 |
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/Lecture 25/Question05.java:
--------------------------------------------------------------------------------
1 | Identify the errors and correct them.
2 |
3 | Hint: Press Submit to see the errors and note the spellings.
4 |
5 | package q11129;
6 | public class Student {
7 | private String id;
8 | private String name;
9 | private int age;
10 | private char gender;
11 |
12 | public Student(String id, String name, int age, char gender) {
13 | this.id = id;
14 | this.name = name;
15 | this.age = age;
16 | this.gender = gender;
17 | }
18 |
19 | public String toString() {
20 | return "Student [name = " + name + ", id = " + id + ", age = " + age + ", gender = " + gender + "]";
21 | }
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/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 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;i 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 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 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 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 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 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 24/Question2.java:
--------------------------------------------------------------------------------
1 | In a class if the name of the constructor is different from the name of the class, Java treats it as a different method.
2 |
3 | Press Submit to see compilation error and then correct the error.
4 |
5 | [Hint: Java is case sensitive, meaning change in the case of characters will make it a different name.]
6 |
7 |
8 | package q11143;
9 | public class Student {
10 | private String id;
11 | private String name;
12 | private int age;
13 | private char gender;
14 | public Student(String id, String name, int age, char gender) {
15 | this.id = id;
16 | this.name = name;
17 | this.age = age;
18 | this.gender = gender;
19 | }
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/Lecture 31/Question02.java:
--------------------------------------------------------------------------------
1 | Write a class PrefixCheck with a main method. The method receives one command line argument. If the argument starts with en print it as it is, else prepend en and print the output.
2 |
3 | These are examples for your understanding :
4 | Cmd Args : bye
5 | enbye
6 | Cmd Args : entertainment
7 | entertainment
8 |
9 |
10 | package q11192;
11 |
12 |
13 | public class PrefixCheck{
14 |
15 | public static void main(String[] args) {
16 |
17 | String ans=args[0];
18 |
19 | if(ans.startsWith("en")) System.out.println(ans);
20 |
21 | else System.out.println("en"+ ans);
22 |
23 |
24 |
25 |
26 |
27 |
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/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/Question03.java:
--------------------------------------------------------------------------------
1 | Write a class ExpandAString with a main method. The program receives two command line arguments. The first argument is a string and the second argument is a number. Print the first argument as many number of times as the second argument.
2 |
3 | For Example:
4 | Cmd Args : Ganga 3
5 | GangaGangaGanga
6 |
7 |
8 | package q11193;
9 | public class ExpandAString {
10 |
11 |
12 |
13 |
14 | public static void main(String[] args) {
15 |
16 | String ans=args[0];
17 |
18 | int n=Integer.parseInt(args[1]);
19 |
20 | System.out.println(args[0].repeat(n));
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/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 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 21/Question18.java:
--------------------------------------------------------------------------------
1 | Write a class CompareArrays with a public method compareArrays that takes two parameters arr1 and arr2 are of type int[] and returns true if the lengths of arr1 and arr2 are equal.
2 |
3 | Here is an example:
4 | Enter lenght of the arr1:
5 | 5
6 | Enter lenght of the arr2:
7 | 5
8 | tru
9 |
10 | package q11066;
11 |
12 | public class CompareArrays {
13 | /**
14 | * Find the length of the two arrays are equal or not
15 | *
16 | *
17 | * @return result
18 | */
19 |
20 |
21 |
22 | public boolean compareArrays(int[] arr1, int[] arr2) {
23 | //Write your code here
24 | boolean ans=(arr1.length==arr2.length)?true:false;
25 | return ans;
26 | }
27 | }
--------------------------------------------------------------------------------
/Lecture 30/Question4.java:
--------------------------------------------------------------------------------
1 | Create a class TestFormatter with a main method. The method receives one command line argument, convert it to an integer.
2 |
3 | Format and print the integer such that it has comma (or locale specific) delimiters, has a minimum width of 6 characters (put spaces in the front if the number is smaller), and has a sign indicator (+ or -).
4 |
5 | [Note: You need not use %n at the end of your formatting text.]
6 |
7 | package q11188;
8 |
9 |
10 | public class TestFormatter{
11 |
12 | public static void main(String[] args) {
13 |
14 | int ans=Integer.parseInt(args[0]);
15 |
16 |
17 |
18 | System.out.printf("%,+6d", ans);
19 |
20 |
21 |
22 |
23 | }
24 | }
--------------------------------------------------------------------------------
/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 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 19/Question7.java:
--------------------------------------------------------------------------------
1 | Retype the code below and submit.
2 |
3 | Write a class ArrayElementCounter with a public method countElement that takes two parameters one is arr of type int[] and the other is element of type int and returns the count of the element that occures in the arr. The return type of countElement should be int.
4 |
5 | Here is an example:
6 | Cmd Args : 3 2 3 3 3 3
7 | The element 3 presents 4 times in the arry
8 |
9 |
10 | package q10942;
11 | public class ArrayElementCounter {
12 | public int countElement(int[] arr, int element) {
13 | int count = 0;
14 | for (int i = 0; i < arr.length; i++) {
15 | if (arr[i] == element) {
16 | count++;
17 | }
18 | }
19 | return count;
20 | }
21 | }
--------------------------------------------------------------------------------
/Lecture 23/Question7.java:
--------------------------------------------------------------------------------
1 | In a class the fields are usually declared with private access modifier. We will learn more about access modifiers in the ensuing sections.
2 |
3 | A field declaration could also contain initialization. For example:
4 | private int age = 25;
5 | Field declarations of similar types can be done as a single statement. For example:
6 | private int age = 25, totalMarks = 100;
7 | Note that every statement must be terminated by a semicolon.
8 |
9 | Identify the errors and correct them.
10 |
11 | Note: Please don't change the package name.
12 |
13 | package q11155;
14 | public class Student {
15 | private String id;
16 | private String name;
17 | private int age;
18 | private char gender;
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/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 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 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 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 19/Question5.java:
--------------------------------------------------------------------------------
1 | Retype and submit the code below.
2 |
3 | Write a class IndexFinder with public method printIndex that takes two parameters one is intArr of type int[] and other is an element of type int and print all indices of the elements in intArr which are equal to the given element.
4 |
5 | Assumptions:
6 | arr is never null
7 | Here is an example:
8 | Cmd Args : 2 2 2 42 2
9 | Indices of the elments matched with the given element 2 is :
10 | 0
11 | 1
12 | 2
13 |
14 | package q10940;
15 | public class IndexFinder {
16 | public void printIndex(int[] intArr, int element) {
17 | for (int i = 0; i < intArr.length; i++) {
18 | if (intArr[i] == element) {
19 | System.out.println(i);
20 | }
21 | }
22 | }
23 | }
24 |
25 |
--------------------------------------------------------------------------------
/Lecture 21/Question04.java:
--------------------------------------------------------------------------------
1 | write a class ElementCount with a main method which passes an arr of type int[] and an element of type int. Print number of times the element is present in the arr.
2 |
3 | Here is an example:
4 | int[] arr = {1, 12, 9, 3, 5, 3, 78, 4, 3, 9, 18, 56, 1, 5}
5 | Cmd Args : 1
6 | 2
7 |
8 |
9 | Note: Please don't change the package name.'
10 |
11 | package q11049;
12 | public class ElementCount {
13 | public static void main(String[] args) {
14 | int[] arr = {1, 12, 9, 3, 5, 3, 78, 4, 3, 9, 18, 56, 1, 5};
15 | int element = Integer.parseInt(args[0]);
16 | int cnt=0;
17 | for(int i=0;i 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 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 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 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 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 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/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 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 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 21/Question24.java:
--------------------------------------------------------------------------------
1 | Write a class FindAverage with a public method findAverage that takes one paremeter arr of type int[] and returns the average of elements in the arr.
2 |
3 | Assumptions:
4 | arr is never null
5 | Here is an example:
6 | Enter no of elements in the arr:
7 | 4
8 | Enter elements in the arr seperated by space:
9 | 1 2 3 4
10 | Avg elements of the array is:
11 | 2
12 |
13 | package q11072;
14 |
15 | public class FindAverage {
16 | /**
17 | * Compute the average of elements in the given array
18 | *
19 | *
20 | * @return result
21 | */
22 |
23 | public int findAverage(int[] arr) {
24 |
25 | int ans=0;
26 | for(int i=0;i nameList = new ArrayList < String > ();
16 |
17 | for (int i = 0; i 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 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 21/Question07.java:
--------------------------------------------------------------------------------
1 | Write a class ElementCheck with a public method elementFinder that takes one parameter arr of type int[] and return true if the first and last elements of the arr are same else return false. The return type of elementFinder should be boolean.
2 |
3 | Assumptions:
4 | arr is never null
5 | Here are examples for your understanding:
6 | Cmd Args : 33 25 12 5 33
7 | true
8 | Cmd Args : 1 2 3 4
9 | false
10 |
11 | package q11055;
12 |
13 | public class ElementCheck {
14 |
15 | /**
16 | * Find first and last elements of the array are same are not
17 | *
18 | * @return true if both are same else return false
19 | */
20 |
21 |
22 | public boolean elementFinder(int[] arr) {
23 |
24 | //Write your code here
25 | if(arr[0]==arr[arr.length-1]){
26 | return true;
27 | }else{
28 | return false;
29 |
30 | }
31 |
32 | }
33 | }
--------------------------------------------------------------------------------
/Lecture 58/Question18.java:
--------------------------------------------------------------------------------
1 | Create a class TreeMapDemo with a main method. The method takes inputs from the command line arguments and make first three chars of the input as a key and argument as a value add them to the TreeMap and print the result.
2 |
3 | Sample Input and Output:
4 | Cmd Args : Samos Dallas Bangalore
5 | Ban : Bangalore
6 | Dal : Dallas
7 | Sam : Samos
8 |
9 |
10 | import java.util.*;
11 | public class TreeMapDemo {
12 | public static void main(String[] args) {
13 | Map 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 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 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 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 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 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 19/Question3.java:
--------------------------------------------------------------------------------
1 | Retype the code below. The class RiverNameFinder has a method findElement(int index). It accepts an integer argument index. The method prints the array element at that index.
2 |
3 | The findElement method creates an array namesArr containing river names. It accesses the element of namesArr at index and prints the element.
4 |
5 | Observe that before accessing the element, the method checks if the index is valid or not by checking if index is greater than zero and less than the size of namesArr. Otherwise, we get an ArrayIndexOutofBoundsException.
6 |
7 | package q10938;
8 | public class RiverNameFinder {
9 | public void findElement(int index) {
10 | String[] namesArr = { "Ganga", "Yamuna", "Godavari", "Krishna", "Narmada", "Kaveri" };
11 | if (index >= 0 && index < namesArr.length) {
12 | System.out.println(namesArr[index]);
13 | }
14 | }
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/Lecture 30/Question5.java:
--------------------------------------------------------------------------------
1 | Create a class TestFormatter with a main method. The method receives one command line argument.
2 |
3 | Convert the argument received in the main method into a double value.
4 |
5 | Format and print the double value such that it has :
6 | comma (or locale specific) delimiters
7 | a minimum width of 12 characters (put numbers in the front if the number is smaller)
8 | only 3 decimal points
9 | a sign indicator (+ or -)
10 | instead of spaces put zeros(0's) to the left if the number is smaller
11 | [Note: You need not use %n at the end of your formatting text.]
12 |
13 |
14 |
15 |
16 | package q11189;
17 |
18 |
19 | public class TestFormatter{
20 |
21 | public static void main(String[] args) {
22 |
23 | double ans=Double.parseDouble(args[0]);
24 |
25 |
26 |
27 | System.out.printf("%,+012.3f", ans);
28 |
29 |
30 |
31 |
32 | }
33 | }
--------------------------------------------------------------------------------
/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/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 29/Question13.java:
--------------------------------------------------------------------------------
1 | Create a class StringSplit with a main method. The method receives two command line arguments. First argument is a string. Second argument is a regular expression.
2 |
3 | A regular expression is a sequence of characters that define a search pattern.
4 |
5 | Example:
6 | +, -, *, ]Split the first argument using the regular expression and print the items. Make sure there are no leading or trailing spaces in the items.
7 |
8 | For Example:
9 | Cmd Args: Rama-Lakshmana-Bharatha-Satrugna -
10 | Rama
11 | Lakshmana
12 | Bharatha
13 | Satrugna
14 |
15 |
16 | package q11183;
17 |
18 |
19 | public class StringSplit {
20 |
21 | public static void main(String[] args) {
22 |
23 | String str = args[0];
24 |
25 | String[] splits = str.split(args[1]);
26 |
27 | for (String s : splits) {
28 |
29 | System.out.println(s.trim());
30 |
31 |
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/Lecture 19/Question8.java:
--------------------------------------------------------------------------------
1 | Create a class ArrayElementCounter with a public method countElement that takes two parameters one is arr of type int[] and second one is element of type int and returns the count of element occures in the arr. The return type of countElement should be int.
2 |
3 | Assumptions:
4 | arr is never null
5 | Here is an example:
6 | Cmd Args : 1 1 2 3 1
7 | 2
8 |
9 | package q10943;
10 |
11 | public class ArrayElementCounter {
12 | /**
13 | * Find number of times the element present in the given array
14 | *
15 | *
16 | *
17 | * @retrun count
18 | */
19 |
20 | public int countElement(int[] arr, int element) {
21 | //Write your code here
22 |
23 |
24 | int count = 0;
25 |
26 | for (int i = 0; i< arr.length; i++) {
27 |
28 | if (arr[i] == element) {
29 |
30 | count++;
31 |
32 | }
33 |
34 | } return count;
35 |
36 |
37 |
38 | }
39 | }
--------------------------------------------------------------------------------
/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 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 25/Question04.java:
--------------------------------------------------------------------------------
1 | The toString() method like the constructor of a class is part of the class body. It must be written inside a class scope (inside the opening and closing brace of the class).
2 |
3 | Hint: You can select some code and press ALT + Up key or ALT + Down key to move the selected code up and down. (Note: This works only in Windows and not in MAC and Linux)
4 |
5 | Identify and correct the error.
6 |
7 |
8 |
9 | package q11128;
10 | public class Student {
11 | private String id;
12 | private String name;
13 | private int age;
14 | private char gender;
15 |
16 | public Student(String name, String id, int age, char gender) {
17 | this.id = id;
18 | this.name = name;
19 | this.age = age;
20 | this.gender = gender;
21 | }
22 |
23 |
24 |
25 | public String toString() {
26 | return "Student [name = " + name + ", id = " + id + ", age = " + age + ", gender = " + gender + "]";
27 | }
28 | }
--------------------------------------------------------------------------------
/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 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 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=0){
28 |
29 |
30 | sum+=arr[i];
31 | }
32 | }
33 | return sum;
34 |
35 | }
36 | }
--------------------------------------------------------------------------------
/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 21/Question13.java:
--------------------------------------------------------------------------------
1 | Write a class FindMiddle with a public method findMiddle that takes one parameter arr of type int[] and print the middle element in the arr
2 |
3 | Assumptions:
4 | arr is never null
5 | arr length is even it should print the middle two numbers
6 | arr length is odd it prints the middle element
7 | Here are examples for your understanding:
8 | Cmd Args : 1 6 3 5 4
9 | 3
10 | Cmd Args : 3 2 1 6 5 4
11 | 1
12 | 6
13 |
14 | package q11061;
15 |
16 | public class FindMiddle {
17 | /**
18 | * Find the middle element in the given array
19 | *
20 | *
21 | *
22 | * @ return element
23 | */
24 | public void findMiddle(int[] arr) {
25 |
26 | //Write your code here
27 | int left=0;
28 | int right=arr.length;
29 | int mid =(right+left)/2;
30 | if(right%2==0)
31 | System.out.println(arr[right/2-1]+"\n"+ arr[right/2]);
32 | else
33 | System.out.println(arr[right/2]);
34 |
35 |
36 | }
37 | }
--------------------------------------------------------------------------------
/Lecture 21/Question21.java:
--------------------------------------------------------------------------------
1 | Write a class CountEvens with a public method countEvens that takes one parameter arr of type int[] and returns the count of even numbers present in the arr.
2 |
3 | Assumptions:
4 | arr is never null
5 | These are examples:
6 | Enter no of elements in the array:
7 | 5
8 | Enter elements in the array seperated by space:
9 | 1 2 3 4 6
10 | 3
11 | Enter no of elements in the array:
12 | 5
13 | Enter elements in the array seperated by space:
14 | 1 3 7 9 5
15 | 0
16 |
17 | package q11069;
18 |
19 |
20 | public class CountEvens {
21 | /**
22 | * Find the count of even numbers in the given array
23 | *
24 | *
25 | *
26 | * @return count
27 | *
28 | */
29 |
30 |
31 | public int countEvens(int[] arr) {
32 | int count=0;
33 | //Write your code here
34 | for(int i=0;isecondNo){
29 |
30 | return true;
31 | }
32 | else{
33 |
34 | return false;
35 |
36 | }
37 |
38 |
39 |
40 |
41 | }
42 | }
--------------------------------------------------------------------------------
/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 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 21/Question01.java:
--------------------------------------------------------------------------------
1 | Write a class SumOfElements with a public method sum that takes one parameter arr of type int[] and returns the sum of all elements in arr. The return type of sum should be long.
2 |
3 | Assumptions:
4 | arr is never null
5 | return 0 in case arr is empty
6 | Here is an example:
7 | Cmd Args : 3 5 3 2 0
8 | Sum of all elements in the given array is : 13
9 | Note how the return type of the function is long and not int. The reason for this is to overcome errors due to data overflow while adding multiple int values.
10 |
11 | package q11046;
12 | public class SumOfElements {
13 |
14 | /**
15 | * Computes the sum of all the elements in the given array
16 | *
17 | *
18 | * @return the sum
19 | */
20 | public long sum(int[] arr) {
21 | // Write the code
22 | if(arr.length==0){
23 | return 0;
24 | }
25 | long sum=0;
26 | for(int i=0;i4){
30 | flag= false;
31 | }else{
32 | return true;
33 | }
34 |
35 | }
36 |
37 | return flag;
38 |
39 |
40 |
41 | }
42 | }
--------------------------------------------------------------------------------
/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/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/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 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 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/Question11.java:
--------------------------------------------------------------------------------
1 | Write a class SequenceCheck with a public method sequenceCheck that takes one parameter arr of type int[] and returns true if 6, 9, 12 present consecutively in the arr. The return type of sequenceCheck should be boolean.
2 |
3 | Assumptions:
4 | arr is never null
5 | Elements 6, 9, 12 are appear consecutiviely
6 | Here are examples:
7 | Cmd Args : 62 32 6 9 12
8 | true
9 | Cmd Args : 99 36 6 12 56 9
10 | false
11 |
12 | package q11059;
13 |
14 | public class SequenceCheck {
15 | /**
16 | * Find the given elements present consecutively in the array or not
17 | *
18 | *
19 | * @return result
20 | */
21 |
22 | public boolean sequenceCheck(int[] arr) {
23 | boolean result=false;
24 |
25 | //Write your code here
26 | 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 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 |
--------------------------------------------------------------------------------
/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 ans = new ArrayList < String > ();
19 |
20 | for(int k = 0; k < args.length;k++) ans.add (args[k]);
21 |
22 | for(int i = 0; i1){
33 | return true;
34 | }
35 | }
36 | }
37 | return false;
38 | }
39 | }
--------------------------------------------------------------------------------
/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 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/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 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 23/Question6.java:
--------------------------------------------------------------------------------
1 | If we consider a vehicle, say a car, we would immediately think of its model, colour, maximum speed etc. These are the various attributes also called properties of an instance of a car whose values could vary from model to model.
2 |
3 | Similarly, when we consider a class say, Student, we can list the most common attributes/properties of a Student class as id, name, age, gender.
4 |
5 | Note the spellings of id, name, age and gender they are written using camel-case convention and they all start with a lower case letter.
6 |
7 | private is an access modifier like public and it is a Java keyword written in lower case.
8 |
9 | int and char are primitive types written in lower case.
10 | String is a class name and hence is written with the starting letter in upper case.
11 |
12 | See the code and retype the same.
13 |
14 | Note: Please don't change the package name.
15 |
16 |
17 | package q11154;
18 | public class Student {
19 | private String id;
20 | private String name;
21 | private int age;
22 | private char gender;
23 | }
--------------------------------------------------------------------------------
/Lecture 25/Question09.java:
--------------------------------------------------------------------------------
1 | package q11134;
2 | public class Student {
3 | private String id;
4 | private String name;
5 | private int age;
6 | private char gender;
7 |
8 | public Student(String id, String name, int age, char gender) {
9 | this.id = id;
10 | this.name = name;
11 | this.age = age;
12 | this.gender = gender;
13 | }
14 |
15 | public void setId(String id) {
16 | this.id = id;
17 | }
18 |
19 | public String getId() {
20 | return id;
21 | }
22 |
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 |
27 | public String getName() {
28 | return name;
29 | }
30 |
31 | public void setAge(int age) {
32 | this.age = age;
33 | }
34 |
35 | public int getAge() {
36 | return age;
37 | }
38 |
39 | public void setGender(char gender) {
40 | this.gender = gender;
41 | }
42 |
43 | public char getGender() {
44 | return gender;
45 | }
46 |
47 | public String toString() {
48 | return "Student [id = " + id + ", name = " + name + ", age = " + age + ", gender = " + gender + "]";
49 | }
50 | }
--------------------------------------------------------------------------------
/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 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 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 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 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 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 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 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 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 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 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 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 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 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 58/Question10.java:
--------------------------------------------------------------------------------
1 | get(Object key): Returns the value to which the key is mapped, or returns null if there is no mapping for the key.
2 |
3 | Write a program to understand how to get the value mapped to the particular key in HashMap using get method.
4 |
5 | Create a class HashMapMethodsDemo with a main method. Create an instance of the HashMap and get the value mapped to the key Tue.
6 |
7 | The result should be as follows:
8 | namesMap = {Thu=Thursday, Tue=Tuesday, Sun=Sunday, Mon=Monday}
9 | value mapped to Tue is : Tuesday
10 |
11 |
12 | package q11968;
13 | import java.util.*;
14 | public class HashMapMethodsDemo {
15 | public static void main(String[] args) {
16 | Map 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 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 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 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 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 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 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 22/Question10.java:
--------------------------------------------------------------------------------
1 | Write a class SequenceOfEvens with a public method checkEvenSequence that takes one parameter arr of type int[] and returns true if three consecutive even numbers are present in the arr. The return type of checkEvenSequence is boolean.
2 |
3 | Assumptions:
4 | arr is never null
5 | Here is an example:
6 | Enter no of elements in the array:
7 | 4
8 | Enter elements in the array seperated by space:
9 | 2 4 6 5
10 | true
11 |
12 | package q11095;
13 |
14 | public class SequenceOfEvens {
15 | /**
16 | * Find three consecutive even numbers are present in the array or not
17 | *
18 | *
19 | *
20 | * @return result
21 | */
22 |
23 | public void checkEvenSequence(int[] arr) {
24 | //Write your code here
25 |
26 | int count=0;
27 |
28 | for (int i=0; i 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 27/Question1.java:
--------------------------------------------------------------------------------
1 | A string literal consists of zero or more characters (also called Unicode code points) enclosed in double quotes. Some examples of string literals are
2 | "Ganga" , "123", "", " ", "Practice makes perfect", " 24@3yApp_"
3 |
4 | String literals are instances of java.lang.String class.
5 |
6 | An instance of String is immutable. Meaning, once a string object is created in memory, the contents of the string cannot be changed. For example, when a string is created as given below :
7 | String str = "Ganga";
8 | The contents of "Ganga" cannot be changed. However, str can be made to point to some other string object like "Yamuna".
9 |
10 | Below program creates some String literals. Retype the program and submit.
11 |
12 | Note: Please don't change the package name.'
13 |
14 |
15 | package q11144;
16 | public class StringLiterals {
17 | public static void main(String[] args) {
18 | String aStr = "Himalayas";
19 | String bStr = "are beautiful";
20 | String spaceStr = " ";
21 | String emptyStr = "";//This is an empty string that does nothing.
22 | System.out.println(aStr + spaceStr + bStr + "." + emptyStr);
23 | }
24 | }
--------------------------------------------------------------------------------
/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 29/Question12.java:
--------------------------------------------------------------------------------
1 | Suppose we have a string that contains a list of comma separated items like, "Apples, Oranges, Peaches, Bananas". We want to split the string extract all the items into an array. String class provides a method for doing this, called public String[] split(String regex).
2 |
3 | The above string can be split using split method like this :
4 | "Apples, Oranges, Peaches, Bananas".split(",").
5 | It returns an array containing the items in the list.
6 | When we print the array, it prints the following:
7 | Apples
8 | Oranges
9 | Peaches
10 | Bananas
11 | Note the extra space before each item (except Apples).
12 | The argument for the split method should be a regular expression. You will learn about using regular expressions in Java in some other section.
13 |
14 | Retype the code below that demonstrates the usage of split method.
15 |
16 |
17 | package q11182;
18 | public class StringMethods {
19 | public static void main(String[] args) {
20 | String str = "http://docs.oracle.com/javase/7/docs/api/";
21 | String[] splits = str.split("/");
22 | for (String s : splits) {
23 | System.out.println(s);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/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 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 && index 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 21/Question03.java:
--------------------------------------------------------------------------------
1 |
2 | L21
3 | Arrays - Problem Solving - 1
4 | Q. No : 11048
5 | Problem solving with Array
6 | Write a class ElementCheck with a public method checkFirstOrLast that takes two parameters one is arr of type int[] and second one is arg of type int and returns true if the arg is first or last element in the arr else returns false. The return type of checkFirstOrLast is boolean.
7 |
8 | Assumption:
9 | arr is never null
10 | Here is an example:
11 | Enter no of elements in the array: 4
12 | Enter elements in the array seperated by space:1 2 3 6
13 | Enter the search element:6
14 | true
15 |
16 | package q11048;
17 |
18 | public class ElementCheck {
19 |
20 | /** write a logic to check whether the given element is present in the first or last in the array
21 | *
22 | *
23 | *
24 | *@return true if the element present if not return false
25 | *
26 | */
27 | public boolean checkFirstOrLast(int[] arr, int arg) {
28 | //Write your code
29 |
30 | if(arr[0]==arg){
31 | return true;
32 | }else{
33 | if(arr[arr.length-1]==arg){
34 | return true;
35 | }else{
36 | return false;
37 | }
38 | }
39 |
40 | }
41 | }
--------------------------------------------------------------------------------
/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 22/Question05.java:
--------------------------------------------------------------------------------
1 | Write a class ReorderArray with a public method reorder that takes one parameter arr of type int[] and returns the arr such that all even numbers in the array come to the front of the arr.
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 | 3 5 6 4 2 4
10 | 6
11 | 4
12 | 2
13 | 4
14 | 3
15 | 5
16 |
17 | package q11090;
18 |
19 | public class ReorderArray {
20 | /**
21 | *Arrange all even numbers to infront of the array
22 | *
23 | *
24 | *
25 | *
26 | * @return result
27 | */
28 |
29 | public int[] reorder(int[] arr) {
30 |
31 | //Write your code here
32 | int[] a = new int[arr.length];
33 |
34 | int j=0;
35 |
36 | for(int i=0; i 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 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 26/Question9.java:
--------------------------------------------------------------------------------
1 | In Java, the void keyword is a placeholder to indicate that there is no return type for a method.
2 |
3 | For example, the below method adds num1 and num2 and returns the result.
4 | public int sum(int num1, int num2) {
5 | return num1 + num2;
6 | }
7 | Since the type of the result being returned is also int, the method's return type is marked int.
8 |
9 | In the below example, you will notice that the method printSum does not return but prints the summation to the System's console.
10 | public void printSum(int num1, int num2) {
11 | System.out.println( num1 + num2);
12 | }
13 | Since the method printSum does not return any value, the return type of the method is void. By this the compiler understands that this method does not return any.
14 |
15 | Considering the above two methods are available, select all the correct statements from the below code:
16 | int total1 = printSum(4, 5); //Statement 1
17 | int total2 = sum(4, 5); //Statement 2
18 | System.out.println(printSum(1, 2)); //Statement 3
19 | System.out.println(sum(1, 2)); //Statement 4
20 |
21 |
22 | Answers
23 |
24 | In Statement 2, the value of total2 = 9.
25 |
26 | Statement 4 prints a value of 3 to console.
--------------------------------------------------------------------------------
/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
--------------------------------------------------------------------------------