Saturday, August 13, 2016

C Code Snippets 1 - Calculating the Sum of Digits



ccode


1



/**
* Author : Berk Soysal
*/

#include <stdio.h>
void main()
{
    int num, temp, digit, sum ;

    printf("Enter a TWO digit number: \n");
    scanf("%d", &num);

    if(num > 9 && num < 100)
    {

        temp = num;
        digit = num % 10;
        num /= 10;
        sum  = num + digit;

        printf("Given number = %d\n", temp);
        printf("Sum of the digits = %d\n", sum);
    }

    else
    {
        printf("\n\n ERROR !! Please Enter a TWO Digit Number !\n\n");
    }

}


Result 



Please leave a comment if you have any questions !
Read more course about C Code:



    Tuesday, August 9, 2016

    C Code Snippets 12 - Unique Bubble Sorting


    Hello everyone, today's C code snippet is about bubble sorting. Bubble sort is the easiest way to sort a collection of numbers. It works by sequentially going through the array and comparing two consecutive values at a time, swapping them if necessary. Then, the process is repeated until no swaps are required.

    Numbers2

    Additionally, our code detects the same values in an array and removes the repeated values. For example, 
    {2,4,6,2,5,2,3,1} will be sorted as {1,2,3,4,5,6}.
    
    /**
    * Author : Berk Soysal
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define N 10
    
    void sort(int *a, int n)
    {
        int k, j, temp;
    
        for(k=0; k<n-1; k++)
            for(j=0; j<n-1; j++)
        // If the previous value is larger, swap.
                if( a[j]>a[j+1] )
                {
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
        // If there are any equal values, keep only one nonzero
                else if(a[j]==a[j+1])
                {
                    a[j]=0;
                }
    }
    
    int main()
    {
        int i, x[N] = {42,12,53,86,27,70,64,70,45,23};
    
        // Print the input
        printf("Input : ");
        for(i=0; i<N; i++)
            printf("%5d",x[i]);
    
        printf("\n");
    
        // Call the sort function
        sort(x, N);
    
        // Print the sorted array
        printf("Sorted: ");
        for(i=0; i<N; i++)
            if(x[i]!=0)
                printf("%5d",x[i]);
    
        printf("\n");
    
        return 0;
    }
    
    

    Output:


    Please leave a comment if you have any questions or comments.
    Read more:


      CSS Code Snippets - Amazing Blurred Text Animation - Pure CSS3 !


      Today, I am going to share a cool code snippet that you can use on your homepage to hypnotize your visitors! You can also use it to promote a product or to give a message or however you like. It looks like this (Without the watermark of course):

      css-text-soysal


      This part of the code is necessary to download the font and needs to be placed in between <head> and </head> of your HTML code; 
      
      <link href="https://fonts.googleapis.com/css?family=Electrolize" rel="stylesheet">
      
      
      The following code is the main part of the animation and could be placed anywhere in the body of your HTML file.

      
      <div class="mycontainer">
        <div class="myheader">
          <div class="clr"></div>
        </div>
        <div class="sp-container">
          <div class="sp-content">
            <div class="sp-globe"></div>
            <h2 class="frame-1">Welcome!</h2>
            <h2 class="frame-2">Write your text here!</h2>
      
            <h2 class="frame-3">And something else here!</h2>
      
            <h2 class="frame-4">Then!</h2>
      
            <h2 class="frame-5"><span>Subscribe,</span> <span>Share,</span> <span>Enjoy!</span></h2>
            <a class="sp-circle-link" href="http://www.soysal.tk"><img width="100" height="40" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD2LqUXacJEcele5uSsmWKcPZua9LfZB31XxPxRUcQ474bCH8e2O5VODTrqiE0DPk3_THBu4W1I7I75lXmLPKpy9U5kcLL1B850h08n7nAnjShuqIQxO1YfdIo8zluIoEf3dq_AdVzPic/s1600/soysal.tk.png" /></a>
      
          </div>
        </div>
      </div>
      
      
      In order to complement the HTML code, this is the CSS code and should be added to your CSS file:
      
      body {
       background: #010863;
       font-family: 'Electrolize', sans-serif;
      }
      
      .mycontainer{
       width: 100%;
       position: relative;
       overflow:hidden;
      }
      
      a {
        text-decoration:none;
      }
      
      h1.main,p.demos {
       -webkit-animation-delay: 18s;
       -moz-animation-delay: 18s;
       -ms-animation-delay: 18s;
       animation-delay: 18s;
      }
      .sp-container {
       position: fixed;
       top: 0px;
       left: 0px;
       width: 100%;
       height: 100%;
       z-index: 0;
       background: -webkit-radial-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.3) 35%, rgba(0, 0, 0, 0.7));
       background: -moz-radial-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.3) 35%, rgba(0, 0, 0, 0.7));
       background: -ms-radial-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.3) 35%, rgba(0, 0, 0, 0.7));
       background: radial-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.3) 35%, rgba(0, 0, 0, 0.7));
      }
      .sp-content {
       position: absolute;
       width: 100%;
       height: 100%;
       left: 0px;
       top: 0px;
       z-index: 1000;
      }
      .sp-container h2 {
       position: absolute;
       top: 50%;
       line-height: 100px;
       height: 90px;
       margin-top: -50px;
       font-size: 90px;
       width: 100%;
       text-align: center;
       color: transparent;
       -webkit-animation: blurFadeInOut 3s ease-in backwards;
       -moz-animation: blurFadeInOut 3s ease-in backwards;
       -ms-animation: blurFadeInOut 3s ease-in backwards;
       animation: blurFadeInOut 3s ease-in backwards;
      }
      .sp-container h2.frame-1 {
       -webkit-animation-delay: 0s;
       -moz-animation-delay: 0s;
       -ms-animation-delay: 0s;
       animation-delay: 0s;
      }
      .sp-container h2.frame-2 {
       -webkit-animation-delay: 3s;
       -moz-animation-delay: 3s;
       -ms-animation-delay: 3s;
       animation-delay: 3s;
      }
      .sp-container h2.frame-3 {
       -webkit-animation-delay: 6s;
       -moz-animation-delay: 6s;
       -ms-animation-delay: 6s;
       animation-delay: 6s;
      }
      .sp-container h2.frame-4 {
       font-size: 200px;
       -webkit-animation-delay: 9s;
       -moz-animation-delay: 9s;
       -ms-animation-delay: 9s;
       animation-delay: 9s;
      }
      .sp-container h2.frame-5 {
       -webkit-animation: none;
       -moz-animation: none;
       -ms-animation: none;
       animation: none;
       color: transparent;
       text-shadow: 0px 0px 1px #fff;
      }
      .sp-container h2.frame-5 span {
       -webkit-animation: blurFadeIn 3s ease-in 12s backwards;
       -moz-animation: blurFadeIn 1s ease-in 12s backwards;
       -ms-animation: blurFadeIn 3s ease-in 12s backwards;
       animation: blurFadeIn 3s ease-in 12s backwards;
       color: transparent;
       text-shadow: 0px 0px 1px #fff;
      }
      .sp-container h2.frame-5 span:nth-child(2) {
       -webkit-animation-delay: 13s;
       -moz-animation-delay: 13s;
       -ms-animation-delay: 13s;
       animation-delay: 13s;
      }
      .sp-container h2.frame-5 span:nth-child(3) {
       -webkit-animation-delay: 14s;
       -moz-animation-delay: 14s;
       -ms-animation-delay: 14s;
       animation-delay: 14s;
      }
      .sp-globe {
       position: absolute;
       width: 282px;
       height: 273px;
       left: 50%;
       top: 50%;
       margin: -137px 0 0 -141px;
       background: transparent url(http://web-sonick.zz.mu/images/sl/globe.png) no-repeat top left;
       -webkit-animation: fadeInBack 3.6s linear 14s backwards;
       -moz-animation: fadeInBack 3.6s linear 14s backwards;
       -ms-animation: fadeInBack 3.6s linear 14s backwards;
       animation: fadeInBack 3.6s linear 14s backwards;
       -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
       filter: alpha(opacity=30);
       opacity: 0.3;
       -webkit-transform: scale(5);
       -moz-transform: scale(5);
       -o-transform: scale(5);
       -ms-transform: scale(5);
       transform: scale(5);
      }
      .sp-circle-link {
       position: absolute;
       left: 50%;
       top: 20px;
       margin-left: -50px;
       text-align: center;
       line-height: 100px;
       width: 100px;
       height: 100px;
       background: black;
       color: #3f1616;
       font-size: 25px;
       -webkit-border-radius: 50%;
       -moz-border-radius: 50%;
       border-radius: 50%;
       -webkit-animation: fadeInRotate 1s linear 16s backwards;
       -moz-animation: fadeInRotate 1s linear 16s backwards;
       -ms-animation: fadeInRotate 1s linear 16s backwards;
       animation: fadeInRotate 1s linear 16s backwards;
       -webkit-transform: scale(1) rotate(0deg);
       -moz-transform: scale(1) rotate(0deg);
       -o-transform: scale(1) rotate(0deg);
       -ms-transform: scale(1) rotate(0deg);
       transform: scale(1) rotate(0deg);
      }
      .sp-circle-link img{
       margin-top:30px;
      }
      .sp-circle-link:hover {
       background: #85373b;
       color: #fff;
      }
      /**/
      @-webkit-keyframes blurFadeInOut{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        -webkit-transform: scale(1.3);
       }
       20%,75%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        -webkit-transform: scale(1);
       }
       100%{
        opacity: 0;
        text-shadow: 0px 0px 50px #fff;
        -webkit-transform: scale(0);
       }
      }
      @-webkit-keyframes blurFadeIn{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        -webkit-transform: scale(1.3);
       }
       50%{
        opacity: 0.5;
        text-shadow: 0px 0px 10px #fff;
        -webkit-transform: scale(1.1);
       }
       100%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        -webkit-transform: scale(1);
       }
      }
      @-webkit-keyframes fadeInBack{
       0%{
        opacity: 0;
        -webkit-transform: scale(0);
       }
       50%{
        opacity: 0.4;
        -webkit-transform: scale(2);
       }
       100%{
        opacity: 0.2;
        -webkit-transform: scale(5);
       }
      }
      @-webkit-keyframes fadeInRotate{
       0%{
        opacity: 0;
        -webkit-transform: scale(0) rotate(360deg);
       }
       100%{
        opacity: 1;
        -webkit-transform: scale(1) rotate(0deg);
       }
      }
      /**/
      @-moz-keyframes blurFadeInOut{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        -moz-transform: scale(1.3);
       }
       20%,75%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        -moz-transform: scale(1);
       }
       100%{
        opacity: 0;
        text-shadow: 0px 0px 50px #fff;
        -moz-transform: scale(0);
       }
      }
      @-moz-keyframes blurFadeIn{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        -moz-transform: scale(1.3);
       }
       100%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        -moz-transform: scale(1);
       }
      }
      @-moz-keyframes fadeInBack{
       0%{
        opacity: 0;
        -moz-transform: scale(0);
       }
       50%{
        opacity: 0.4;
        -moz-transform: scale(2);
       }
       100%{
        opacity: 0.2;
        -moz-transform: scale(5);
       }
      }
      @-moz-keyframes fadeInRotate{
       0%{
        opacity: 0;
        -moz-transform: scale(0) rotate(360deg);
       }
       100%{
        opacity: 1;
        -moz-transform: scale(1) rotate(0deg);
       }
      }
      /**/
      @keyframes blurFadeInOut{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        transform: scale(1.3);
       }
       20%,75%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        transform: scale(1);
       }
       100%{
        opacity: 0;
        text-shadow: 0px 0px 50px #fff;
        transform: scale(0);
       }
      }
      @keyframes blurFadeIn{
       0%{
        opacity: 0;
        text-shadow: 0px 0px 40px #fff;
        transform: scale(1.3);
       }
       50%{
        opacity: 0.5;
        text-shadow: 0px 0px 10px #fff;
        transform: scale(1.1);
       }
       100%{
        opacity: 1;
        text-shadow: 0px 0px 1px #fff;
        transform: scale(1);
       }
      }
      @keyframes fadeInBack{
       0%{
        opacity: 0;
        transform: scale(0);
       }
       50%{
        opacity: 0.4;
        transform: scale(2);
       }
       100%{
        opacity: 0.2;
        transform: scale(5);
       }
      }
      @keyframes fadeInRotate{
       0%{
        opacity: 0;
        transform: scale(0) rotate(360deg);
       }
       100%{
        opacity: 1;
        transform: scale(1) rotate(0deg);
       }
      }
      
      
      Alright guys, that's all. Please leave a comment below if you have any questions or comments.
      Read more:

      Monday, August 8, 2016

      40+ Essential Core Java Interview Questions - Updated Regularly!

      Hey everyone,

      In this post I am going to share some valuable Core Java Interview Questions..

      java-interview-questions

      These questions are not only good for practicing for your technical interviews, they are also good for improving your Java knowledge or remembering the details of the language. This post will be updated regularly so please do check it from time to time. Here are the questions:
      1 - What does the following Java program print?
      The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0.
      2 - What gives Java its 'write once and run anywhere' nature?
      The Bytecode
      3 - Can a constructor be final?
      No, constructors cannot be final.
      4 - What's the difference between an interface and an abstract class?
      Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
      http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
      5 - What's the difference between constructors and other methods?
      Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
      6 - Can you call one constructor from another if a class has multiple constructors?
      Yes. Use this() syntax.
      7 - What is method overriding?
      If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for run-time polymorphism and to provide the specific implementation of the method.
      8 - What are the field/method access levels (specifiers) and class access levels?
      Each field and method has an access level: • private: accessible only in this class • (package): accessible only in this package • protected: accessible only in this package and in all subclasses of this class • public: accessible everywhere this class is available Similarly, each class has one of two possible access levels: • (package): class objects can only be declared and manipulated by code in this package • public: class objects can be declared and manipulated by code in any package
      9 - Why would you use a synchronized block vs. synchronized method?
      Synchronized blocks place locks for shorter periods than synchronized methods.
      10 - How can you force garbage collection?
      You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.
      11 - How do you know if an explicit object casting is needed?
      If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example: Object a; Customer b; b = (Customer) a; When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.
      12 - What is the output of the following program?

      Static method called
      We can call static methods using reference variable which is pointing to null because static methods are class level so we can either call using class name and reference variable which is pointing to null.
      13 - Describe the wrapper classes in Java?
      Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type. Following table lists the primitive types and the corresponding wrapper classes: 
      Primitive Wrapper 
      boolean java.lang.Boolean 
      byte java.lang.Byte 
      char java.lang.Character 
      double java.lang.Double 
      float java.lang.Float 
      int java.lang.Integer 
      long java.lang.Long 
      short java.lang.Short 
      void java.lang.Void
      14 - What's the difference between the methods sleep() and wait()?

      Thread.sleep() causes the current thread to suspend execution for a specified period. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS. 
      Object.wait() causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. 
      "The Thread.sleep method is not guaranteed to be precise so it may not be exactly the specified duration. Also, sleep can end early if the thread is interrupted. I think it would be more correct to say sleep suspends execution at least the specified time unless interrupted and wait suspends up to the specified time." Brooks Hagenow

      15 - What's the main difference between a Vector and an ArrayList?
      Java Vector class is internally synchronized and ArrayList is not.
      16 - Can we write a Java class that could be used both as an applet as well as an application?
      Yes. We can add a main() method to the applet.
      17 - What would you use to compare two String variables? The operator "==" or the method "equals()"?
      I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.
      18 - What is the output of the following program?

      false
      true

      256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer class. So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Otherwise it will be different (for 128).
      19 - Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
      Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
      20 - Can an inner class be declared inside of a method access local variables of this method?
      It's possible if these variables are final.
      21 - What can go wrong if you replace && with & in the following code: String a=null; if (a!=null && a.length()>10) {...}
      A single ampersand here would lead to a NullPointerException.
      22 - What's the difference between a queue and a stack?
      Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.
      23 - For concatenation of strings, which method is good, StringBuffer or String?
      StringBuffer is faster and more memory efficient than String for concatenation.
      24 - How can a subclass call a method or a constructor defined in a superclass?
      Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.
      25 - What is a collection?
      A collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable.
      26 - You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
      Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.
      27 - What is the difference between StringBuffer and StringBuilder ?
      StringBuffer is synchronized whereas StringBuilder is not synchronized.
      28 - What is the difference between Collection and Collections?
      Collection is an interface whereas Collections is a class. Collection interface provides normal functionality of data structure to List, Set and Queue. But, Collections class is to sort and synchronize collection elements.
      29 - What is the output of the following code?
      The given print statement will throw java.lang.NullPointerExceptionbecause while evaluating the OR logical operator it will first evaluate both the literals and since str is null, .equals() method will throw exception. It's always advisable to use short circuit logical operators i.e "||" and "&&" which evaluates the literals values from left and since the first literal would return true, it would skip the second literal evaluation and the result would be true in that case.
      30 - What is deadlock? When does it occur?
      Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
      31 - If you're overriding the method equals() of an object, which other method you might also consider?
      hashCode() method.
      32 - You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
      ArrayList
      33 - How would you make a copy of an entire Java object with its state?
      Have this class implement Cloneable interface and call its method clone().
      34 - What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
      You do not need to specify any access level, and Java will use a default package access level.
      35 - When do you declare a method as an abstract method?
      When i want child class to implement the behavior of the method.
      36 - Can I call an abstract method from a non abstract method?
      Yes, We can call a abstract method from a Non abstract method in a Java abstract class.
      37 - What is the difference between checked and Unchecked Exceptions in Java?
      Checked exceptions must be caught using try .. catch() block or we should be declared using 'throws' clause. If you don't, compilation of the program will fail. However unchecked exceptions let the program compile but might cause a run-time exception after compilation.
      38 - What are the uses of Serialization?
      In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers. These are some of the typical uses of serialization: 
      • To persist data for future use. 
      • To send data to a remote computer using such client/server Java technologies as RMI or socket programming. 
      • To "flatten" an object into array of bytes in memory. 
      • To exchange data between applets and servlets. 
      • To store user session in Web applications. 
      • To activate/passivate enterprise java beans. 
      • To send objects between the servers in a cluster.
      39 - What is the Runnable interface ? Are there any other ways to make a java program multi-threaded ?
      There are two ways to create new kinds of threads: 
      - Define a new class that extends the Thread class 
      - Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor. 
      - An advantage of the second approach is that the new class can be a subclass of any class, not just of the Thread class.
      40 - Is the following code valid in Java? Is it an example of method overloading or overriding? 
      If a class have multiple methods by same name but different parameters, it is known as Method Overloading. In this example, method overloading is not possible by changing the return type of the method since ambiguity might occur in the program.

      You have reached the end of the interview questions! These questions are updated regularly. Please leave a comment below if you have any questions or comments! Happy Coding!
      Read more:
      Java Programming For Beginners
      Complete Java For Selenium WebDriver And Test Automation