Find Interview Questions for Top Companies
Ques:- 1.What are the different ways to initialize a variable. How to differentiate global and local variables, explain it through a simple tcl program. 2.Create a list of week days and print the first and last character of each day using foreach command 3.Can you write a small program to verify the given input is file or directory.Before checking, just ensure that the file/dir exists or not in the given path. If the given input is a file, findout the size and verify that the file has all read ,write and execute permission.
Asked In :-
Right Answer:
1. In Tcl, a variable can be initialized in several ways:
- Using the `set` command: `set varName value`
- Direct assignment: `set varName "value"`
- Using `array` for associative arrays: `array set myArray {key value}`

To differentiate between global and local variables, you can use the `global` command for global variables. Here's a simple program:

```tcl
proc exampleProc {} {
set localVar "I am local"
global globalVar
set globalVar "I am global"
puts $localVar
puts $globalVar
}
exampleProc
puts $globalVar ;# This will print "I am global"
```

2. To create a list of weekdays and print the first and last character of each day using the `foreach` command:

```tcl
set weekdays {Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
Comments
Admin May 17, 2020

2. % set days {monday tuesday wednasday thursday friday
saturday sunday}
monday tuesday wednasday thursday friday saturday sunday
% foreach day $days {
set str [split $day ""]
puts "[lindex $str 0]"
puts "[lindex $str end]"
}

Admin May 17, 2020

1.
a. Initializing using
set x 10 
lappend y 13
b. Initializing using out put of an expression 
set z [expr $x + $y ]
c . Initializing using a list
set m [list $x $y $z]

Ques:- How to get the next ip for given ip ex: 10.10.10.1 -> 10.10.10.2 ex: 10.10.10.255 -> 10.10.11.0
Asked In :-
Right Answer:
```tcl
proc next_ip {ip} {
set octets [split $ip "."]
set last_octet [lindex $octets 3]
set third_octet [lindex $octets 2]

if {$last_octet < 255} {
incr last_octet
} else {
set last_octet 0
incr third_octet
}

return "[lindex $octets 0].[lindex $octets 1].$third_octet.$last_octet"
}
```
Comments
Admin May 17, 2020

set ip "10.10.10.255"
set list [split $ip "."]
set ww [lindex $list 0]
set xx [lindex $list 1]
set yy [lindex $list 2]
set zz [lindex $list 3]
if {$zz>254} {
set zz 0
incr yy
if {$yy>255} {
set yy 0
incr xx
if {$xx>255} {
set xx 0
incr ww }
if {$ww>255} {
set ww 0
}}} else {
incr zz
}
puts "$ww.$xx.$yy.$zz"

Admin May 17, 2020

proc next_ipv4 {a} {
regexp {([0-9]+.[0-9]+.[0-9]+.[0-9]+)} $a b
set b [split $b .]
if {[lindex $b 3] < 255} {
set c [lindex $b 3]
set c [expr $c +1]
set b [lreplace $b 3 3 $c]
set b [join $b .]
puts "next IP is $b"
} elseif {[lindex $b 2] < 255} {
set c [lindex $b 2]
set c [expr $c +1]
set b [lreplace $b 2 3 $c 1]
set b [join $b .]
puts "Next IP is $b"
} elseif {[lindex $b 1] < 255} {
set c [lindex $b 1]
set c [expr $c +1]
set b [lreplace $b 1 3 $c 0 1]
set b [join $b .]
puts "next IP is $b"
} elseif {[lindex $b 0] < 255} {
set c [lindex $b 0]
set c [expr $c +1]
set b [lreplace $b 0 3 $c 0 0 1]
set b [join $b .]
puts "next IP is $b"
} else {
puts "Last IPv4 address is 255.255.255.255"
}
}

Ques:- Write a proc to increment the ip by the given no. of times. The incremented IPs should be a valid one. Ex: proc {ip no_of_incrments} { body }
Asked In :-
Right Answer:
```tcl
proc ip {ip no_of_increments} {
set ip_parts [split $ip "."]
for {set i 0} {$i < $no_of_increments} {incr i} {
set last_part [lindex $ip_parts 3]
set last_part [expr {$last_part + 1}]
if {$last_part > 255} {
set last_part 0
set ip_parts [lmap part $ip_parts {expr {($part + ($last_part == 0 ? 1 : 0)) % 256}}]
} else {
set ip_parts [lreplace $ip_parts 3 3 $last_part]
}
}
return [join $ip_parts "."]
}
```
Comments
Admin May 17, 2020

Thanks jasmin for your valuable script.
small correction for "if" conditions .
proc increment_ip {ip no_of_inc} {
puts "Ip address --- $ip"
for {set inc 1} {$inc<=$no_of_inc} {incr inc} {
set ip_list [split $ip "."]
set oct1 [lindex $ip_list 0]
set oct2 [lindex $ip_list 1]
set oct3 [lindex $ip_list 2]
set oct4 [lindex $ip_list 3]
incr oct4
if {$oct4>255} {
set oct4 0
incr oct3
}
if {$oct3>255} {
set oct3 0
incr oct2
}
if {$oct2>255} {
set oct2 0
incr oct1
}
if {$oct1>255} {
incr oct1 -1
puts "cannot increment ip"
exit
}
set ip $oct1.$oct2.$oct3.$oct4
puts "next ip -- $ip"
}
}
increment_ip 1.1.1.1 10

Admin May 17, 2020

proc increment_ip {ip no_of_inc} {
puts "ip..... $ip"
for {set inc 1} {$inc<=$no_of_inc} {incr
inc} {
set ip_list [split $ip .]
set oct1 [lindex $ip_list 0]
set oct2 [lindex $ip_list 1]
set oct3 [lindex $ip_list 2]
set oct4 [lindex $ip_list 3]
incr oct4
if {$oct4>255} {
set oct4 0
incr oct3
if
{$oct3>255} {
set oct3 0
incr oct2
if {$oct2>255} {
set oct2 0
incr oct1
if {$oct1>255} {
incr oct1 -1
puts "cannot increment
ip"
exit
}
}
}
}
set ip $oct1.$oct2.$oct3.$oct4
}
puts "new ip .... $oct1.$oct2.$oct3.$oct4"
}
increment_ip 10.10.10.1 10



AmbitionBox Logo

What makes Takluu valuable for interview preparation?

1 Lakh+
Companies
6 Lakh+
Interview Questions
50K+
Job Profiles
20K+
Users