Friday, April 24, 2020

Python - IO functions

1. How the print function differs in Python 3 and 2 version?
Most notable and most widely known change in Python 3 is how the print function is used. Use of parenthesis () with print function is now mandatory. It was optional in Python 2.
  • print "Hello World" #is acceptable in Python 2
  • print ("Hello World") # in Python 3, print must be followed by () 
2. How the reading input functions differ in Python 3 and 2 version?
  • Python 2 has two versions of input functions, input() and raw_input(). The input() function treats the received data as string if it is included in quotes '' or "", otherwise the data is treated as number.
  • In Python 3, raw_input() function is deprecated. Further, the received data is always treated as string.
3. Major difference between Python 2 and Python 3:
Basis of comparison
Python 3
Python 2
Release Date
2008
2000
Function print
print ("hello")
print "hello"
Division of Integers
Whenever two integers are divided, you get a float value
When two integers are divided, you always provide integer value.
Unicode
In Python 3, default storing of strings is Unicode.
To store Unicode string value, you require to define them with "u".
Syntax
The syntax is simpler and easily understandable.
The syntax of Python 2 was comparatively difficult to understand.
Rules of ordering Comparisons
In this version, Rules of ordering comparisons have been simplified.
Rules of ordering comparison are very complex.
Iteration
The new Range() function introduced to perform iterations.
In Python 2, the xrange() is used for iterations.
Exceptions
It should be enclosed in parenthesis.
It should be enclosed in notations.
Leak of variables
The value of variables never changes.
The value of the global variable will change while using it inside for-loop.
Backward compatibility
Not difficult to port python 2 to python 3 but it is never reliable.
Python version 3 is not backwardly compatible with Python 2.
Library
Many recent developers are creating libraries which you can only use with Python 3.
Many older libraries created for Python 2 is not forward-compatible.


4. 

Introduction to Python programming

1. What is Python?
Python is a popular programming language. It was created in 1991 by Guido van Rossum.
It is used for:
  • web development (server-side),
  • software development,
  • mathematics,
  • system scripting.

2.What can Python do?
  • used on a server to create web applications. 
  • used alongside software to create workflows. 
  • connect to database systems. It can also read and modify files. 
  • used to handle big data and perform complex mathematics. 
  • used for rapid prototyping, or for production-ready software development.

3.Why Python?
  • Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
  • Python has a simple syntax similar to the English language.
  • Python has syntax that allows developers to write programs with fewer lines than some other programming languages.
  • Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.
  • Python can be treated in a procedural way, an object-orientated way or a functional way.

4. Good to know about Python:
  • The most recent major version of Python is Python 3, which we shall be using in this tutorial. However, Python 2, although not being updated with anything other than security updates, is still quite popular.
  • In this tutorial Python will be written in a text editor. It is possible to write Python in an Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are particularly useful when managing larger collections of Python files.

5. How Python Syntax differs from other programming languages?
  • Python was designed to for readability, and has some similarities to the English language with influence from mathematics.
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.
  • Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.

Saturday, April 18, 2020

JAVA Programming - 4.1 OOPS basics - Introduction and Encapsulation

1. What is Encapsulation?
Encapsulation is the process of wrapping data member, member functions and member classes inside a single module called class, that defines the restriction of an object and its member variables and methods.

2. Can a class be defined inside an another class?
Yes. Class can be defined inside another class, that is called inner class. This increases the degree of encapsulation. Static class defined inside another class called Nested inner class which can be accessed without outer class instance; but Non-static defined inside another class (called inner class) can only be accessed with outer class instance.

3. What is constructor and define its function?
Constructor is a method defined inside a class which has same name as class name and it has no return time; It is used to initialize instance variable of class. If the constructor defined as private we can't create object instance of a class.

4. What will be the output of the following JAVA program?
class LWD{
public static void learn(){
System.out.println("Learning");
}
public LWD(){
System.out.println("Wondering");
}
public static void main(String l[]){
learn();
new LWD();
System.out.println("Developing");
}

}
Output & Explanation:
Learning
Wondering
Developing
Here, LWD class have two static(learn() & main()) and a constructor LWD(); Program execution start with main method which calls learn() prints "Learning" and LWD() calls prints "Wondering"; main() method prints "Developing".
Note:
learn(); // Non static method call doesn't requires object instance of the class LWD.
listen(); // Static method call requires object instance of the class LWD.


5. What will be the output of the following JAVA program?
class LWD{
class Online{
public static void main(String l[]){
System.out.println("Welcome to JAVA class");
}
}
}
Output & Explanation:
compilation error due to illegal declaration of static method inside non static inner class. Static method and variable only declared inside static block, so the inner class should be the static class.
class LWD{
static class Online{
public static void main(String l[]){ System.out.println("Welcome to JAVA class");}
}}
where output is "Welcome to JAVA class".

6. How many class files created when the following java code complied?
class AP{}
class TN{}
class College{
class Student{}
}
Answer & Explanation:
4 class files, namely
class AP{} → AP.class from AP class
class TN{} → TN.class from TN class
class College{ → College.class  from Outer class College
class Student{}→ College$Student.class from Inner class Student
}

7. What is Singleton class?
A class can have only one object instance is called Singleton class. It provides a global point of access to the object.
Singleton class used in logging, caches, thread pools, configuration settings, device driver objects.


Rules to implement singleton class:
  • Constructor should be private
  • Declare a static variable object of the class
  • Declare a static method to return the instance


8. How garbage collector knows that the object is not in use and needs to be removed?
Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.

9. What is wrapper class in java?
Wrapper classes helps to convert primitives into object(Autoboxing) and object into primitives(Unboxing).

Example:
boolean→Boolean
byte→Byte
char→Character
int→Integer
float→Float
long→Long
short→Short

10. What is System.out in Java?


11. Is null a keyword in java?
 Yes. It is a reserved word(keyword) for a literal values, it is similar to true and false and used to represent the empty value or points to nothing.

12. What is java static import?

In 1.5 versions, according to sun static import improves readability of the code but according to worldwide programming exports static imports creates confusion and reduces readability of the code. Hence if there is no specific requirement never recommended to use a static import.

Without static method:
public class Main {
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
}
}
With static method:
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) {
System.out.println(sqrt(4));
}
}

Wednesday, April 15, 2020

Java Programming - 4.2 Looping statement

1. What will the output of following JAVA program?
public class LWD{
public static void main(String [] args){
int a=0;
for(System.out.print("Learn with ");a<1;System.out.println("Durga")){
a++;
}
System.out.println("Never stop learning");
       }
}
Output & Explanation:
Learn with Durga

2. What will the output of following JAVA program?
public class LWD{
public static void main(String [] args){
for(int x=0;x<20;x++);
System.out.println(x);
       }
}
Output & Explanation: compile time error
In the above JAVA code, for loop end with semi colon for(int x=0;x<20;x++); denotes empty for loop for(int x=0;x<20;x++){ }; Scope of variable x,  only inside for loop, next line x doesn't declare in LWD class.   
class LWD{
public static void main(String [] args){
int x;
for(x=0;x<20;x++);
System.out.println(x);
       }

}

3. What will be the output of following JAVA program?
class LWD{
    public static void main(String l[]){
        int a[]={10,20,30,40,50};
        for(int i=0; i<a.length;i++)
            System.out.print(a[i]+" ");
    }
}
A. Error – length is not a method of a.
B. 6
C. 10,20,30,40,50
D. 10 20 30 40 50
Output & Explanation:
10 20 30 40 50

4. What is the correct syntax of for each loop?
  1. for(variable:collection){body;}
  2. for(data_type variable:collection){body;}
  3. for(variable;collection){body;}
  4. for(data_type variable;collection){body;}
Answer: 
2
for(data_type variable:collection)
{body;}

5. Determine output:
public class LWD{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}
A. 10
B. 11
C. 9
D. 20
E. None of these
Answer: A

6. How many times will the following code print “Learn with Durga"? int count = 0;
do {
System.out.println("Learn with Durga");
count++;
} while (count < 10);

A. 8
B. 9
C. 10
D. 11
E. 0
Answer: C

7. Choose the correct statement in context of the following program code. public class LWD{
public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d<10 would always be true.
D. The program compiles and runs fine.
Answer: D

8. What is the value of a[1] after the following code is executed? int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

A. 0
B. 1
C. 2
D. 3
E. 4 
Answer: B

9. Which of the following for loops will be an infinite loop? A. for(; ;)
B. for(i=0 ; i<1; i--)
C. for(i=0; ; i++)
D. All of the above
Answer: D


10. public class While { 
public void loop() { 
int x= 0; 
while ( 1 ) /* Line 6 */ 
{ System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } 
Which statement is true?

A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6.
C. There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6.
Answer: D

Saturday, April 11, 2020

Java Programming - 1.4 Operators

1. What will be the output of the following JAVA code?
class LWD{
public static void main(String arg[]){
int a=b=c=d=9;
System.out.println(a+" "+b+" "+c+" "+d);
}
}
Output & Explanation:
Compile time error
In the above JAVA code, variable a is declared and initialized with the integer value 9, rest of the variable b, c and d are not declared. Hence it shows can't find the symbol error during compilation time.
In-order to avoid this error, we can declare and initialize the variables as mentioned below:
int a,b,c,d; 
a=b=c=d=9; // valid declaration

2. What will be the output of the following JAVA code?
class  LWD{
        public static void main(String args[])
        {
int l,d;
l=9;
d=7;
float l1,d1;
l1=9.0f;
d1=7.0f;
int result[]={l+d,l-d,l*d,l/d,l%d};
float result1[]={l1+d1,l1-d1,l1*d1,l1/d1,l1%d1};
for(int i=0;i<result.length;i++){
System.out.println("Int result & float result1:"+result[i]+" & "+result1[i]);
}
       } 

}
Output & Explanation:
Int result & float result1: 16 & 16.0
Int result & float result1: 2 & 2.0
Int result & float result1: 63 & 63.0
Int result & float result1: 1 & 1.2857143
Int result & float result1: 2 & 2.0

3. What will be the output of the following JAVA code?
class LWD{
      public static void main(String args[]){   
             int a,b;
a=5;b=6; 
int result[]={a & b,a|b ,a^b};
for(int i=0;i<result.length;i++)
System.out.println("result="+result[i]);
      }
 }



Output & Explanation:
result=4
result=7

result=3

A
B
Bitwise &
Bitwise |
Bitwise ^
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0
Given that, 
5 - 0101
  0101
  0110
  0101
  0110
  0101
  0110
6 - 0110
Result
&0100à4
| 0111 à7
^ 0011 à3

4. What will be the output of the following JAVA code?
class LWD{
        public static void main(String args[]){   
           byte a=1;
   a=a+1;
   System.out.println(a);
        }

    }
Output & Explanation:
Compile time error due to incompitable type, lossy conversion from int to byte.
byte+byte=int, integer can't be stored in byte variable.

5. What will be the output of the following JAVA code?
class A{}
class B{}
class LWD extends A{
public static void main(String args[]){
A a=new LWD();
B b=new B();
LWD l=new LWD();
System.out.println(a instanceof LWD); 
System.out.println(b instanceof B);
System.out.println(l instanceof LWD);
}
}
Output & Explanation:
false
true
true
instanceof operator used to check the object is comes from the respective class instance or not.

6. What will be the output of the following JAVA code?
class Samsung{
static int i=5;
void disp(){
System.out.println(i--);
System.out.println(i--);
}
}
class Mobile{
public static void main(String arg[]){
Samsung m1=new Samsung();
m1.disp();
Samsung m2=new Samsung();
m2.disp();
}
}

Output & Explanation:
5
4
3
2
Static variable is used to initialize the variable only once in the program.  disp() function called with m1 and m2 instance, m1.disp() prints twice the value of i--(i--=5 and i--=4) and being statice variable m2.disp() prints again twice the value of i-- (i-- =3 and i-- =2). 

7. What will be the output of the following JAVA code?
class LWD{
public static void main(String L[]){
int x=20,y=15;
if(++x>10 || ++y>15)
x++;
else
y++;
System.out.println(x+" "+y);
}
}
Output & Explanation:
22 15

Bitwise &, |
Logical &&, ||
Both arguments should be evaluated always.
x & y à Even if x is false, y will also be evaluated.
x | y à Even if x is true, y will also be evaluated.
Second argument evaluation is optional.
x&&y à If x is true then only y will be evaluated.
x || y à If x is false the only y will be evaluated.
Relatively performance is low
Relatively performance is high
Applicable for integer and Boolean types.
Applicable only for Boolean types but not for integer types.
Given that, x=20 and y=15
Operator
x
Y
&&
22
16
||
22
15
&
22
16
|
22
16


Friday, April 10, 2020

Java Programming - 1.3 IO Operations


1. Which of these class is not related to input and output stream in terms of functioning?
A. File
B. Writer
C. InputStream
D. Reader
Answer: Option A
Explanation: A File describes properties of a file, a File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time date, and directories path, and to navigate sub directories.


2. What will be the output of following program ?
class LWD{
public static void main(String[] args){
System.out.print("Keep on");
System.out.println("Learning");
}
}
A. Keep onLearning
B. Keep on Learning
C. Keep on
       Learning
D. Compile time error.
Answer & Explanation: A
Keep onLearning
System.out.print() does not print new line after printing string, while System.out.println(); prints new line after printing string. Hence output will be Keep onLearning and then new line.


3. What will be the output of following program ?
class LWD{
public static void main(String args[]){
const int a=10;
System.out.println(a);
}
}
A. 10
B. a
C. Unprintable Character
D. Compile time error
Answer and Explanation: D
Complie time error: due to illegal start of expression. JAVA does not support the const keyword, instead of const, final keyword is used.

4. What will be the output of following program ?
class LWD{
public static void main(String[] args){
char a=0x47;//Unicode of 'G'
char b=0x44;//Unicode of 'D'
System.out.print(a+"" + b+"");
System.out.print("-");
System.out.print(a+b);
}
}
A. GD-GD
B. GD-139
C. GD-Error
D. G D-139
Answer & Explanation: B
GD-139
a+"" and b+"" will be converted into string, .toString() or +"" after variable or value converts value into the string and a+b will be added because they are not converted into string. Hence output will be GD-139.

5. What will be the output of following program ?
class LWD{
public static void main(String[] lwd){
int a,b,c,d;
a=Integer.parseInt(lwd[1]);
b=Integer.parseInt(lwd[2]);
c=Integer.parseInt(lwd[3]);
d=Integer.parseInt(lwd[4]);
System.out.println(a+b+c+d);
}
}
Output & Explanation:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at LWD.main(MCQ_5.java:4)

6. What will be the output of following program ?
public class MCQ_1{
    public static void main(String args[]){
        System.out.print('A' + 'B');
    }
}
A. AB
B. 195
C. 131
D. Error
Answer & Explanation: C
131
Here, ‘A’ and ‘B’ are not strings they are characters. ‘A’ and ‘B’ will not concatenate. The Unicode of the ‘A’ and ‘B’ will add. The Unicode of ‘A’ is 65 and ‘B’ is 66. Hence Output will be 131.

7. What will be the output of following program ?
public class MCQ_2{
    public static void main(String args[]){
        System.out.print("A" + "B" + 'A');
    }
}
A. ABA
B. AB65
C. Error
D. AB
Answer & Explanation: A
ABA
If you try to concatenate any type of data like integer, character, float with string value, the result will be a string. So 'A' will be concatenated with "AB" and answer will be "ABA".

8. Which is the correct declaration of a boolean variable? 
A. boolean isAdult='false';
B. boolean isAdult=0;
C. boolean isAdult="false";
D. boolean isAdult=false;

Answer and Explanation: D
boolean isAdult=false;A boolean variable can store only true or false.